ZF-9944: Zend_Form_Element_File loses breakChainOnFailure option if NotEmpty validator is not supplied

Description

If I create a file upload form element with several validators, setting {{$breakChainOnFailure}} for each of them, the chain doesn't break and several error messages are shown.

I've spent hours to find the reason and I've found it. This is the faulty part of {{Zend_Form_Element_File::isValid()}}'s body:


if (!$this->isRequired()) {
    $adapter->setOptions(array('ignoreNoFile' => true), $this->getName());
} else {
    $adapter->setOptions(array('ignoreNoFile' => false), $this->getName());
    if ($this->autoInsertNotEmptyValidator() and
           !$this->getValidator('NotEmpty'))
    {
        $validators = $this->getValidators();
        $notEmpty   = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true);
        array_unshift($validators, $notEmpty);
        $this->setValidators($validators);
    }
}

As you can see, this code checks if the element is set to be required and if it is and {{NotEmpty}} validator is not supplied, it gets all already set validators, adds {{NotEmpty}} to them, and sets them back. Unfortunately is loses {{$breakChainOnFailure}} option of all those previously set validators during this process.

The temporary workaround to this bug is to explicitly add {{NotEmpty}} validator to the element.

Comments

Sorry i cannot reproduce this in current trunk and 1.10 release branch with the following setup:


public function testAction() {                                                                                                                           
         $form = new Zend_Form();
         $form->addElement('file',
                           'avatar',
                           array('label' => 'Avatar',
                                 'description' => 'Avatar Description',
                                 'required' => true,
                                 'validators' => array(array('Extension',
                                                             true,
                                                             array(false,
                                                                   'png,jpg,jpeg,gif',
                                                                   'messages' => array('fileExtensionFalse' => 'Wrong File Extension'))),
                                                       array('Size',
                                                             true,
                                                             array('max' => 5242880,
                                                                   'messages' => array('fileSizeTooBig' => 'Filesize too big'))))));
         $form->addElement('submit','submit');

         if ($this->getRequest()->isPost()) {
             $form->isValid($this->getRequest()->getParams());
         }
         die($form->render());
     }

Could you provide a simple Testcase that does fail for you?

This is the simplified version of my form which is enough to reproduce the bug:


class Form_User_Avatar extends Zend_Form
{
    public function init()
    {
        $this->addElement('file', 'avatar', array(
            'label' => 'Avatar',
            'required' => true,
            'validators' => array(
                array('Extension', true, array(
                    false,
                    'png,jpg,jpeg,gif'
                )),
                array('Size', true, array(
                    'max' => 5242880
                ))
            )
        ));

        $this->addElement('submit', 'submit');
    }
}

Try to upload a file which is larger than the size limit of this form (5M) but is smaller {{than post_max_size}} (in my case it's 8M).

Assigning to Thomas and updating Components

New feature implemented with r22372