ZF-12053: The 'Identical' validator on a Form's text element appears to compare filtered to unfiltered value.

Issue Type: Bug Created: 2012-02-11T03:45:58.000+0000 Last Updated: 2012-03-13T23:47:18.000+0000 Status: Open Fix version(s): Reporter: Rich Peterson (sootsnoot) Assignee: Thomas Weidner (thomas) Tags: - Zend_Form

  • Zend_Validate
  • zend-form-element
  • zf-crteam-review

Related issues: Attachments:


We use lowercased email addresses for account names. Creating a new account involves a form with two text fields for email address, the second one to confirm spelling. Since we lowercase the names anyway for storage (we don't want to allow different accounts to differ only in alphabetic case), we use the StringToLower filter on both fields. But when we use the Identical validator on the confirmation field, specifying the token to compare to as the email field, I've verified with Xdebug that the validator compares the filtered value of the token (email) field to the unfiltered value of the confirmation field. The end result for a user that tends to spell their email address in mixed case is that the validation always fails, but when the form re-displays with the error message, in fact the original field and the confirmation field show as being identical because by that time both have been filtered. If this isn't a bug in the code, it surely deserves some mention in the documentation...

Here is the code we were using:

<pre class="literal">
    $email = new Zend_Form_Element_Text('email');
    $email->setLabel('Email ');
    $email->setOptions(array('class' => 'field-input-signup'))
          ->addValidator('NotEmpty', true,array(
            'messages' => array(
                => $this->getView()->translate("Please enter email address"))))
          ->addValidator('EmailAddress', true);

    $cemail = new Zend_Form_Element_Text('cemail');
    $cemail->setLabel('Confirm Email ');
    $cemail->setOptions(array('class' => 'field-input-signup'))
           ->addValidator('NotEmpty', true,array(
             'messages' => array(
                 => $this->getView()->translate("Please enter confirm email address"))))
           ->addValidator('Identical', false, array(
             'token' => 'email',
             'messages' => array(
                  => $this->getView()->translate(
                     "'Email' and 'Confirm Email' do not match. Please try again."))));

IMHO, given the name 'Identical', I would expect it always to compare the unfiltered values.


Posted by Adam Lundrigan (adamlundrigan) on 2012-02-22T21:38:53.000+0000

I don't have much experience with Zend_Form, so I may be doing it wrong, but I am seeing a different problem against trunk: even identical lowercased value comparisons fail. When I run this unit test, which is very similar to your reproduction case:

<pre class="highlight">
Index: tests/Zend/Form/ElementTest.php
--- tests/Zend/Form/ElementTest.php     (revision 24624)
+++ tests/Zend/Form/ElementTest.php     (working copy)
@@ -2189,6 +2189,27 @@
         $validator = $username->getValidator('regex');
+    /**
+     * @group ZF-12053
+     */
+    public function testIdenticalValidatorComparesFilteredFormElementValuesCorrectly()
+    {
+        $f1 = new Zend_Form_Element('f1');
+        $f1->addFilter('StringToLower');
+        $f2 = new Zend_Form_Element('f2');
+        $f2->addFilter('StringToLower')
+           ->addValidator('Identical', false, array(
+               'token' => 'f1',
+           ));
+        $form = new Zend_Form(array('elements' => array($f1,$f2)));
+        // Identically-cased values matching filtered result entered in each field
+        $this->assertTrue($form->isValid(array('f1'=>'foobar', 'f2'=>'foobar')));
+        // Differently-cased values entered in each field
+        $this->assertTrue($form->isValid(array('f1'=>'FooBar', 'f2'=>'FOOBAR')));
+    }

 class Zend_Form_ElementTest_Decorator extends Zend_Form_Decorator_Abstract

I get a failure at the first assert. Even when I follow the exact example in the manual (here) I get the same result.

bq. IMHO, given the name 'Identical', I would expect it always to compare the unfiltered values.

I don't see any mention of it in the manual, but the docblock for Zend_Form_Element::isValid has this:

<pre class="highlight">
Note: The *filtered* value is validated.

Posted by Kristofer Karlsson (grizzm0) on 2012-03-10T08:08:53.000+0000

I just noticed this on my own site and I too found it weird that the identical field is validated against the unfiltered value. Using StringToLower as filter and identical validator on field2 I'll add some examples below.

Fails as field2 will be validated as "test" against "TEST" from field1. Field1: TEST Field2: TEST

Fails as field2 will be validated as "test" against "Test" from field1. Field1: Test Field2: test

Success as both fields will be "test" during validation. Field1: test Field2: Test

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.