ZF 2.4 File Validator Required False Doesn't Work
Asked Answered
L

1

6

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.

Lemos answered 14/5, 2015 at 6:27 Comment(0)
E
6

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

Exeat answered 26/6, 2015 at 4:4 Comment(1)
The trick here is to specify the type of \Zend\InputFilter\FileInput. There are some key differences which you can read about in the link above.Iraq

© 2022 - 2024 — McMap. All rights reserved.