Issue Details (XML | Word | Printable)

Key: ZF-6708
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Alexander Veremyev
Reporter: Alexander Veremyev
Votes: 0
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Google issue summary
Zend Framework

Zend_Filter_Input skips ALLOW_EMPTY metacommand processing if any validator is applied to the field

Created: 18/May/09 05:48 AM   Updated: 25/Oct/09 10:31 AM   Resolved: 22/May/09 08:03 AM
Return to search "Fixed in 1.5.1"
Component/s: Zend_Filter_Input
Affects Version/s: None
Fix Version/s: 1.8.2

Time Tracking:
Not Specified


 Description  « Hide

Zend_Filter_Input skips ALLOW_EMPTY metacommand processing if any validator is applied to the field:

/**
 * @param array $validatorRule
 * @return void
 */
protected function _validateRule(array $validatorRule)
{
    ....
    /**
     * Evaluate the inputs against the validator chain.
     */
    if (count((array) $validatorRule[self::FIELDS]) > 1) {
        if (!$validatorRule[self::VALIDATOR_CHAIN]->isValid($data)) {
            $this->_invalidMessages[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getMessages();
            $this->_invalidErrors[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getErrors();
            return;
        }
    ....
foreach ($field as $value) {
    if (empty($value)) {
        if ($validatorRule[self::ALLOW_EMPTY] == true) {
            continue;
        }
        if ($validatorRule[self::VALIDATOR_CHAIN_COUNT] == 0) {
            $notEmptyValidator = $this->_getValidator('NotEmpty');
            $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldKey));
            $validatorRule[self::VALIDATOR_CHAIN]->addValidator($notEmptyValidator);
        }
    }


Alexander Veremyev added a comment - 18/May/09 06:09 AM

That was probably done to avoid 'empty value' messages duplication since these messages may come from Validators as well as from Zend_Filter_Input::ALLOW_EMPTY metacommand.

There are two options:

  • Zend_Filter_Input should interact with Validators in terms of messages semantics and skip their 'empty string' messages.
  • Throw all messages. So user will be responsibile to turn on Zend_Filter_Input::ALLOW_EMPTY (which is off by default) for specified field.

Alexander Veremyev added a comment - 18/May/09 07:59 AM

Right behavior is to skip any validation and throw NOT_EMPTY_MESSAGE if empty value is not allowed, but present.


Alexander Veremyev added a comment - 22/May/09 08:03 AM

Fixed


Kevin McArthur added a comment - 16/Sep/09 02:34 PM

I think this may have introduced a backwards compatibility break. Scenario:

$validation = array(
  'offset' => array (
     'digits',
     'presence' => 'required'
  )
)

$params = new Zend_Filter_Input(null, $validation, $this->_getAllParams());

Prior to this change, integer values of zero would correctly pass. After this change a zero value results in an empty field error.


Matthew Weier O'Phinney added a comment - 17/Sep/09 11:59 AM

Behavior now fixed in trunk and 1.9 release branch; integer 0 is now considered non-empty by the NotEmpty validator.


Simon Corless added a comment - 25/Oct/09 10:31 AM

This fix with the NotEmpty validator has caused me nothing but trouble as it has broken a number of my forms. I used the fact that 0 in PHP is considered empty to validate lists where element 0 is a title thus not required.

I feel that the NotEmpty validator should behave in exactly the same way as the php function empty() with regards to integer 0. For the time being I have extended the notEmpty validator to disallow integer 0.

Should we fix this or implement another validator to cover such situations?