ZF 2.4文件验证器必需虚假不起作用

时间:2015-05-14 06:27:12

标签: zend-framework zend-framework2 zend-form zend-validate zend-file

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

这是我的表单对象

$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)

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

1 个答案:

答案 0 :(得分:5)

更改过滤器

$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