Description

I couldn't find any previous bugs like this, so hopefully it's not a duplicate. The problem is that, if you have two validators where the first uses NotEmpty and the second one doesn't, it throws the NotEmpty error of the first.

Your input to this is a valid float for field1 and an empty string for field2. You run it through Zend_Filter_Input, and it doesn't validate. If you get the messages for field2, it'll return: [field1] => Array ( [isEmpty] => Field1 is empty. ) It also seems to ignore any defaults set in an $options for Zend_Filter_Input.

FWIW, my guess is the issue lies in Zend/Filter/Input.php::878-879.

Comments

Posted by Greg (ggilbert) on 2011-03-10T19:43:42.000+0000

Bart, just to clarify: the problem isn't in the NotEmpty validator - it's in the one following it.

Thanks for watching! I realize the problem is in the combination. However, when I put together a UnitTest to verify the bug, I use a very simple form with two fields and a NotEmpty validator on the first element. Nothing else, just to isolate the problem you described.

To my surprise, the form validates regardless of the values passed into it. So I now have a problem: is that a bug or something I am doing wrong in the test. In any case, it is obviously not the best way to test, because it involves a form, which does not give us the desired isolation level for this test.

Then I also wondered what Zend_Filter_Input actually does in this context? Are you using the validators to configure the input filter? You probably do, so I will have to narrow my test function around this scenario. It is also something I did not do before, so my next step will be figuring out the desired behavior with validators that do work.

Finally, I confirmed and fixed this one. It is too late at night now to create a patch, I will try to supply one tomorrow.

I also found a complete section of code, with the "$unifiedMessages" that I could disable without getting any failures in the UnitTests. So wether this is undertested functionality, or it is completely pointless code. Maybe Ralph can look at my comments in Zend_Filter_Input, once I have provided the patch and the UnitTest.

The key to solving this problem was in the suggested lines of code. The default Not empty message must be preserved and restored before re-entering the loop of $_validationRules. Otherwise, the not empty message of any given NotEmpty validator will persist in any required field that comes after it.

Workaround
If for now, anyone wants a workaround: provide your NotEmpty validators as the last ones in the chain and you will not have a problem.

Added a patch for the unit test for Zend_Filter_Input that add a test case that proves this bug and a patch that fixes the bug.

I the fixing patch I chose to preserve the default not empty message in a variable and restore it at the end of each iteration over the validation rules. Other approaches may be possible, but this works.