Explorar el Código

[ZF-8906] Zend_Validate_Identical:

- added non-strict validation (ZF-8906)
- added form validation

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22075 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas hace 15 años
padre
commit
a5f8ffdd54

+ 66 - 8
documentation/manual/en/module_specs/Zend_Validate-Identical.xml

@@ -18,6 +18,13 @@
         <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>
@@ -76,18 +83,69 @@ if ($valid->isValid($input)) {
             <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>.
-            </para>
-
-            <para>
-                This is also the case for Form Elements. They are objects or arrays. So you can't
-                simply compare a Textfield which contains a password with an textual password
-                from another source. The Element itself is given as array which also contains
-                additional informations.
+                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>
 

+ 43 - 12
library/Zend/Validate/Identical.php

@@ -42,7 +42,7 @@ class Zend_Validate_Identical extends Zend_Validate_Abstract
      * @var array
      */
     protected $_messageTemplates = array(
-        self::NOT_SAME      => "The token '%token%' does not match the given token '%value%'",
+        self::NOT_SAME      => "The two given tokens do not match",
         self::MISSING_TOKEN => 'No token was provided to match against',
     );
 
@@ -59,6 +59,7 @@ class Zend_Validate_Identical extends Zend_Validate_Abstract
      */
     protected $_tokenString;
     protected $_token;
+    protected $_strict = true;
 
     /**
      * Sets validator options
@@ -72,16 +73,28 @@ class Zend_Validate_Identical extends Zend_Validate_Abstract
             $token = $token->toArray();
         }
 
-        if (is_array($token) && (count($token) == 1) && array_key_exists('token', $token)) {
-            $token = $token['token'];
-        }
+        if (is_array($token) && array_key_exists('token', $token)) {
+            if (array_key_exists('strict', $token)) {
+                $this->setStrict($token['strict']);
+            }
 
-        if (null !== $token) {
+            $this->setToken($token['token']);
+        } else if (null !== $token) {
             $this->setToken($token);
         }
     }
 
     /**
+     * Retrieve token
+     *
+     * @return string
+     */
+    public function getToken()
+    {
+        return $this->_token;
+    }
+
+    /**
      * Set token against which to compare
      *
      * @param  mixed $token
@@ -95,13 +108,24 @@ class Zend_Validate_Identical extends Zend_Validate_Abstract
     }
 
     /**
-     * Retrieve token
+     * Returns the strict parameter
      *
-     * @return string
+     * @return boolean
      */
-    public function getToken()
+    public function getStrict()
     {
-        return $this->_token;
+        return $this->_strict;
+    }
+
+    /**
+     * Sets the strict parameter
+     *
+     * @param Zend_Validate_Identical
+     */
+    public function setStrict($strict)
+    {
+        $this->_strict = (boolean) $strict;
+        return $this;
     }
 
     /**
@@ -111,19 +135,26 @@ class Zend_Validate_Identical extends Zend_Validate_Abstract
      * matches that token.
      *
      * @param  mixed $value
+     * @param  array $context
      * @return boolean
      */
-    public function isValid($value)
+    public function isValid($value, $context = null)
     {
         $this->_setValue((string) $value);
-        $token        = $this->getToken();
+
+        if (($context !== null) && isset($context) && array_key_exists($this->getToken(), $context)) {
+            $token = $context[$this->getToken()];
+        } else {
+            $token = $this->getToken();
+        }
 
         if ($token === null) {
             $this->_error(self::MISSING_TOKEN);
             return false;
         }
 
-        if ($value !== $token)  {
+        $strict = $this->getStrict();
+        if (($strict && ($value !== $token)) || (!$strict && ($value != $token))) {
             $this->_error(self::NOT_SAME);
             return false;
         }

+ 1 - 1
resources/languages/de/Zend_Validate.php

@@ -208,7 +208,7 @@ return array(
     "'%value%' has failed the IBAN check" => "Die IBAN Prüfung ist für '%value%' fehlgeschlagen",
 
     // Zend_Validate_Identical
-    "The token '%token%' does not match the given token '%value%'" => "Der Token '%token%' stimmt nicht mit dem angegebenen Token '%value%' überein",
+    "The two given tokens do not match" => "Die zwei angegebenen Token stimmen nicht überein",
     "No token was provided to match against" => "Es wurde kein Token angegeben gegen den geprüft werden kann",
 
     // Zend_Validate_InArray

+ 1 - 1
resources/languages/en/Zend_Validate.php

@@ -208,7 +208,7 @@ return array(
     "'%value%' has failed the IBAN check" => "'%value%' has failed the IBAN check",
 
     // Zend_Validate_Identical
-    "The token '%token%' does not match the given token '%value%'" => "The token '%token%' does not match the given token '%value%'",
+    "The two given tokens do not match" => "The two given tokens do not match",
     "No token was provided to match against" => "No token was provided to match against",
 
     // Zend_Validate_InArray

+ 9 - 0
tests/Zend/Validate/IdenticalTest.php

@@ -145,6 +145,15 @@ class Zend_Validate_IdenticalTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($validator->isValid(123));
         $this->assertFalse($validator->isValid(array('token' => 123)));
     }
+
+    public function testValidatingNonStrictToken()
+    {
+        $validator = new Zend_Validate_Identical(array('token' => 123, 'strict' => false));
+        $this->assertTrue($validator->isValid('123'));
+
+        $validator->setStrict(true);
+        $this->assertFalse($validator->isValid(array('token' => '123')));
+    }
 }
 
 // Call Zend_Validate_IdenticalTest::main() if this source file is executed directly.