ZF 2.4 文件验证器需要 False 不起作用

ZF 2.4 File Validator Required False Doesn't Work

今天我更新到 ZF 2.4 以使用浮动验证器,但不幸的是我意识到我的文件上传表单字段给出了意外的错误消息。

这是我的表单对象

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

这是我的验证器

$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',
                        ]
                    ],
                ]
            ]);

如您所见,图片上传字段不是必填字段,可能为空。但是在我的表格中我得到了这些错误:

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)

我该如何处理这个问题?我需要这个字段是可选的。

更改过滤器

$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',
                    ]
                ],
            ]
        ]);

在这里阅读:http://framework.zend.com/manual/current/en/modules/zend.input-filter.file-input.html