Zend Framework 多输入一个验证器

Zend Framework multiple input one validator

我正在 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
                        ]
                    ]
                ]
            ]); 

你应该看看 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);