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