Zend_Validate-Identical.xml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.identical">
  4. <title>Identical</title>
  5. <para>
  6. <classname>Zend_Validate_Identical</classname> allows you to validate if a given value is
  7. identical with an set haystack.
  8. </para>
  9. <sect3 id="zend.validate.set.identical.options">
  10. <title>Supported options for Zend_Validate_Identical</title>
  11. <para>
  12. The following options are supported for <classname>Zend_Validate_Identical</classname>:
  13. </para>
  14. <itemizedlist>
  15. <listitem>
  16. <para>
  17. <emphasis><property>strict</property></emphasis>: Defines if the validation
  18. should be done strict. The default value is <constant>TRUE</constant>.
  19. </para>
  20. </listitem>
  21. <listitem>
  22. <para>
  23. <emphasis><property>token</property></emphasis>: Sets the token with which the
  24. input will be validated against.
  25. </para>
  26. </listitem>
  27. </itemizedlist>
  28. </sect3>
  29. <sect3 id="zend.validate.set.identical.basic">
  30. <title>Basic usage</title>
  31. <para>
  32. To validate if two values are identical you need to set the origin value as haystack.
  33. See the following example which validates two strings.
  34. </para>
  35. <programlisting language="php"><![CDATA[
  36. $valid = new Zend_Validate_Identical('origin');
  37. if ($valid->isValid($value) {
  38. return true;
  39. }
  40. ]]></programlisting>
  41. <para>
  42. The validation will only then return <constant>TRUE</constant> when both values are
  43. 100% identical. In our example, when <varname>$value</varname> is 'origin'.
  44. </para>
  45. <para>
  46. You can set the wished token also afterwards by using the method
  47. <methodname>setToken()</methodname> and <methodname>getToken()</methodname> to get
  48. the actual set token.
  49. </para>
  50. </sect3>
  51. <sect3 id="zend.validate.set.identical.types">
  52. <title>Identical objects</title>
  53. <para>
  54. Of course <classname>Zend_Validate_Identical</classname> can not only validate strings,
  55. but also any other variable type like Boolean, Integer, Float, Array or even Objects.
  56. As already noted Haystack and Value must be identical.
  57. </para>
  58. <programlisting language="php"><![CDATA[
  59. $valid = new Zend_Validate_Identical(123);
  60. if ($valid->isValid($input)) {
  61. // input appears to be valid
  62. } else {
  63. // input is invalid
  64. }
  65. ]]></programlisting>
  66. <note>
  67. <title>Type comparison</title>
  68. <para>
  69. You should be aware that also the type of a variable is used for validation.
  70. This means that the string <emphasis>'3'</emphasis> is not identical with the
  71. integer <emphasis>3</emphasis>. When you want such a non strict validation you
  72. must set the <property>strict</property> option.
  73. </para>
  74. </note>
  75. </sect3>
  76. <sect3 id="zend.validate.set.identical.formelements">
  77. <title>Form elements</title>
  78. <para>
  79. <classname>Zend_Validate_Identical</classname> supports also the comparison of form
  80. elements. This can be done by using the element's name as <property>token</property>.
  81. See the following example:
  82. </para>
  83. <programlisting language="php"><![CDATA[
  84. $form->addElement('password', 'elementOne');
  85. $form->addElement('password', 'elementTwo', array(
  86. 'validators' => array(
  87. array('identical', false, array('token' => 'elementOne'))
  88. )
  89. ));
  90. ]]></programlisting>
  91. <para>
  92. By using the elements name from the first element as <property>token</property> for the
  93. second element, the validator validates if the second element is equal with the first
  94. element. In the case your user does not enter two identical values, you will get an
  95. validation error.
  96. </para>
  97. </sect3>
  98. <sect3 id="zend.validate.set.identical.strict">
  99. <title>Strict validation</title>
  100. <para>
  101. As mentioned before <classname>Zend_Validate_Identical</classname> validates tokens
  102. strict. You can change this behaviour by using the <property>strict</property> option.
  103. The default value for this property is <constant>TRUE</constant>.
  104. </para>
  105. <programlisting language="php"><![CDATA[
  106. $valid = new Zend_Validate_Identical(array('token' => 123, 'strict' => FALSE));
  107. $input = '123';
  108. if ($valid->isValid($input)) {
  109. // input appears to be valid
  110. } else {
  111. // input is invalid
  112. }
  113. ]]></programlisting>
  114. <para>
  115. The difference to the previous example is that the validation returns in this case
  116. <constant>TRUE</constant>, even if you compare a integer with string value as long
  117. as the content is identical but not the type.
  118. </para>
  119. <para>
  120. For convinience you can also use <methodname>setStrict()</methodname> and
  121. <methodname>getStrict()</methodname>.
  122. </para>
  123. </sect3>
  124. <sect3 id="zend.validate.set.identical.configuration">
  125. <title>Configuration</title>
  126. <para>
  127. As all other validators also <classname>Zend_Validate_Identical</classname> supports
  128. the usage of configuration settings as input parameter. This means that you can
  129. configure this validator with an <classname>Zend_Config</classname> object.
  130. </para>
  131. <para>
  132. But this adds one case which you have to be aware. When you are using an array as
  133. haystack then you should wrap it within an '<property>token</property>' key when
  134. it could contain only one element.
  135. </para>
  136. <programlisting language="php"><![CDATA[
  137. $valid = new Zend_Validate_Identical(array('token' => 123));
  138. if ($valid->isValid($input)) {
  139. // input appears to be valid
  140. } else {
  141. // input is invalid
  142. }
  143. ]]></programlisting>
  144. <para>
  145. The above example validates the integer 123. The reason for this special case is, that
  146. you can configure the token which has to be used by giving the
  147. '<property>token</property>' key.
  148. </para>
  149. <para>
  150. So, when your haystack contains one element and this element is named
  151. '<property>token</property>' then you have to wrap it like shown in the example below.
  152. </para>
  153. <programlisting language="php"><![CDATA[
  154. $valid = new Zend_Validate_Identical(array('token' => array('token' => 123)));
  155. if ($valid->isValid($input)) {
  156. // input appears to be valid
  157. } else {
  158. // input is invalid
  159. }
  160. ]]></programlisting>
  161. </sect3>
  162. </sect2>
  163. <!--
  164. vim:se ts=4 sw=4 et:
  165. -->