0

I'm designing a form with 3 file upload fields in Zend 2, is it possible to write one validator for 3 fields? I want to use this validator for 3 fields

 $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 Answer 1

1

You should take a look at the ValidatorChain's.

    // 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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.