ZF-5102: Count validator does not work correctly when setMultiFile() is used.


Using this code:

$element = new Zend_Form_Element_File('foo');
$element->setLabel('Upload an image:')
        ->addValidator('Count', false, array('min' => 2, 'max' => 3))
$form->addElement($element, 'foo');

And validating using:

$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
    $uploadedData = $form->getValues();

If you submit the form with two of the file input elements set, then the validation fails with the error: Too less files, minimum '2' are expected but '1' are given

I suspect that if you submitted with all 4 file input elements set, then the max would fail to validate too.


No, max validation works as expected.

The problem is not that the count validation would not work, the problem is that Zend_Form calls the validation on each element individually.

According to the Count Validator you have to validate all files at once when you are validating for a minimum value. This is due to the fact that when you validate, the validator sees that you want to have minimum 2, but only one given. It does not know if you want to call this validator again for another element or not. So this validation fails when called first.

That's the reason why this error is displayed. And I would not know how this situation could be solved.

From the user's point of view there is one Zend_Form_Element_File.

If this problem cannot be resolved, then the option of 'min' should be removed from the Count validator. Also, the manual should be updated to stop implying that it should work. I'm not sure that max has any benefit in any real-world use-case, so maybe Count should just be removed completely.

Fixed with r13807 in trunk. Count validator works now as expected even with multifiles.