ZF-9246: Problem with counting of files which are uploading to server

Description

I found a bug related to counting of files (maximum and minimum) which we can send to the server. For example, this code:


        $upload = new Zend_Form_Element_File('img');
        $upload->addValidator('Count', false, array('min' => 2, 'max' => 3))
               ->setMultiFile(3);

...or this...


        $upload = new Zend_Form_Element_File('img');
        $upload->addValidator('Count', false, array('min' => 0, 'max' => 1));

...won't function properly (doesn't show an error) if you send a empty form. I prepared the appropriate FIX, which eliminates the above error. In the file Zend/File/Transfer/Adapter/Abstract.php (lines 615-629) instead of:


        foreach($check as $key => $content) {
            if (array_key_exists('validators', $content) &&
                in_array('Zend_Validate_File_Count', $content['validators'])) {
                $validator = $this->_validators['Zend_Validate_File_Count'];
                if (array_key_exists('destination', $content)) {
                    $checkit = $content['destination'];
                } else {
                    $checkit = dirname($content['tmp_name']);
                }

                $checkit .= DIRECTORY_SEPARATOR . $content['name'];
                $validator->addFile($checkit);
                $count = $content;
            }
        }

should be:


        foreach($check as $key => $content) {
            if (array_key_exists('validators', $content) &&
                in_array('Zend_Validate_File_Count', $content['validators'])) {
        $validator = $this->_validators['Zend_Validate_File_Count'];
        $count = $content;
        if (empty($content['name'])) continue; /* FIX */
                if (array_key_exists('destination', $content)) {
                    $checkit = $content['destination'];
                } else {
                    $checkit = dirname($content['tmp_name']);
                }

                $checkit .= DIRECTORY_SEPARATOR . $content['name'];
                $validator->addFile($checkit);
            }
        }

And in the file Zend/Validate/File/Count.php (lines 242-263) instead of:


    public function isValid($value, $file = null)
    {
        if (($file !== null) && !array_key_exists('destination', $file)) {
            $file['destination'] = dirname($value);
        }

        if (($file !== null) && array_key_exists('tmp_name', $file)) {
            $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
        }

        $this->addFile($value);
        $this->_count = count($this->_files);
        if (($this->_max !== null) && ($this->_count > $this->_max)) {
            return $this->_throw($file, self::TOO_MANY);
        }

        if (($this->_min !== null) && ($this->_count < $this->_min)) {
            return $this->_throw($file, self::TOO_FEW);
        }

        return true;
    }

should be:


    public function isValid($value, $file = null)
    {
        if (($file !== null) && !array_key_exists('destination', $file)) {
            $file['destination'] = dirname($value);
        }

        if (($file !== null) && array_key_exists('tmp_name', $file)) {
            $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
        }

    if (!empty($file['name'])) { /* FIX */
            $this->addFile($value);
    }
        $this->_count = count($this->_files);
        if (($this->_max !== null) && ($this->_count > $this->_max)) {
            return $this->_throw($file, self::TOO_MANY);
        }

        if (($this->_min !== null) && ($this->_count < $this->_min)) {
            return $this->_throw($file, self::TOO_FEW);
        }

        return true;
    }

I hope that bug will be fixed soon

Comments

Your fix breaks the component. It adds failures which did not exist before.

Setting "min" to "0" I would not know why there should be a failure when sending an empty form. "0" is identical to no file available, and if the form shows a failure depends on how you build your form.

How does your form / elements look like?

For example:

1.


$upload = new Zend_Form_Element_File('img');
$upload->addValidator('Count', false, array('min' => 1, 'max' => 1))

a) empty form (without uploaded file)

ZF 1.10.2 - doesn't show error, but should return ZF + FIX - return an error "Too few files, minimum '1' are expected but '0' are given"

2.


$upload = new Zend_Form_Element_File('img');
$upload->addValidator('Count', false, array('min' => 2, 'max' => 3))
    ->setMultiFile(3);

a) empty form

ZF 1.10.2 - return an error "Too few files, minimum '2' are expected but '1' are given" - but it's wrong because I don't send any files ZF + FIX - return an error "Too few files, minimum '2' are expected but '0' are given" - OK

b) 1 uploaded file

ZF 1.10.2 - doesn't show error, but should return ZF + FIX - return an error "Too few files, minimum '2' are expected but '1' are given" - OK

Fixed with r21208