Zend Framework多输入一个验证器

时间:2018-03-25 09:14:23

标签: php zend-framework2 zend-framework3

我在Zend 2中设计了一个包含3个文件上传字段的表单,是否可以为3个字段编写一个验证器?我想在3个字段中使用此验证器

 $inputFilter->add([
                'type'     => 'Zend\InputFilter\FileInput',
                'name'     => 'foto1',  // Element's name.
                'required' => true,    // Whether the field is required.
                'validators' => [      // Validators.
                    ['name'    => 'FileUploadFile'],
                    [
                        'name'    => 'FileMimeType',                        
                        'options' => [                            
                            'mimeType'  => ['image/jpeg', 'image/png' ]
                        ]
                    ],
                    ['name'    => 'FileIsImage'],
                    [
                        'name'    => 'FileImageSize',
                        'options' => [
                            'minWidth'  => 128,
                            'minHeight' => 128,
                            'maxWidth'  => 4096,
                            'maxHeight' => 4096
                        ]
                    ],
                ],
                'filters'  => [        // Filters.
                    [
                        'name' => 'FileRenameUpload',
                        'options' => [  
                            'target' => './data/upload',
                            'useUploadName' => true,
                            'useUploadExtension' => true,
                            'overwrite' => true,
                            'randomize' => false
                        ]
                    ]
                ]
            ]); 

1 个答案:

答案 0 :(得分:1)

你应该看一下ValidatorChain

    // Creating a re-usable chain
    $chain = new ValidatorChain();
    $chain->attachByName('FileMimeType', [
        'mimeType'  => ['image/jpeg', 'image/png' ]
    ]);
    $chain->attachByName('FileImageSize', [
        'minWidth'  => 128,
        'minHeight' => 128,
        'maxWidth'  => 4096,
        'maxHeight' => 4096
    ]);
    $chain->attachByName('FileRenameUpload', [
        'target' => './data/upload',
        'useUploadName' => true,
        'useUploadExtension' => true,
        'overwrite' => true,
        'randomize' => false
    ]);

    $this->get('foto1')->setValidatorChain($chain);
    $this->get('foto2')->setValidatorChain($chain);
    $this->get('foto3')->setValidatorChain($chain);