zend-frameworkzend-framework2zend-formzend-validatezend-file

ZF 2.4 File Validator Required False Doesn't Work


Today I updated to ZF 2.4 to use float validator but unfortunately i realized that my file upload form field gives unexpected error messages.

Here is my form object

$this->add([
            'name' => 'profileimage',
            'type' => '\Zend\Form\Element\File',
            'attributes' => [
                'id' => 'profileimage',
                'class' => 'styled',
            ],
                ]
        );

And Here is my validator

$inputFilter->add([
                'name' => 'profileimage',
                'required' => false,
                'allow_empty' => true,
                'priority' => 300,
                'filters' => [
                    ['name' => 'StripTags'],
                    ['name' => 'StringTrim'],
                ],
                'validators' => [
                    [
                        'name' => '\Zend\Validator\File\IsImage',
                    ],
                    [
                        'name' => '\Zend\Validator\File\UploadFile',
                    ],
                    [
                        'name' => '\Zend\Validator\File\ImageSize',
                        'options' => [
                            'minWidth' => 300,
                            'minHeight' => 300,
                        ]
                    ],
                    [
                        'name' => '\Zend\Validator\File\Size',
                        'options' => [
                            'max' => '20MB',
                        ]
                    ],
                ]
            ]);

As you see the image upload field is not required and may be empty. But in my form I get these errors:

array (size=1)
  'profileimage' => 
    array (size=4)
      'fileIsImageNotReadable' => string 'File is not readable or does not exist' (length=38)
      'fileUploadFileErrorNoFile' => string 'File was not uploaded' (length=21)
      'fileImageSizeNotReadable' => string 'File is not readable or does not exist' (length=38)
      'fileSizeNotFound' => string 'File is not readable or does not exist' (length=38)

How can I handle this issue? I need to this field to be optional.


Solution

  • change your filter

    $inputFilter->add([
                'name' => 'profileimage',
                'type' => '\Zend\InputFilter\FileInput',
                'required' => false,
                'allow_empty' => true,
                'priority' => 300,
                'filters' => [
                    ['name' => 'StripTags'],
                    ['name' => 'StringTrim'],
                ],
                'validators' => [
                    [
                        'name' => '\Zend\Validator\File\IsImage',
                    ],
                    [
                        'name' => '\Zend\Validator\File\UploadFile',
                    ],
                    [
                        'name' => '\Zend\Validator\File\ImageSize',
                        'options' => [
                            'minWidth' => 300,
                            'minHeight' => 300,
                        ]
                    ],
                    [
                        'name' => '\Zend\Validator\File\Size',
                        'options' => [
                            'max' => '20MB',
                        ]
                    ],
                ]
            ]);
    

    read about it here: http://framework.zend.com/manual/current/en/modules/zend.input-filter.file-input.html