Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 653
Fuat Sayra ÖZDEN Avatar asked May 14 '15 06:05

Fuat Sayra ÖZDEN


1 Answers

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

like image 142
Kien Nguyen Ngoc Avatar answered Oct 16 '22 16:10

Kien Nguyen Ngoc