somejkuser
somejkuser

Reputation: 9040

Add instantiated validtor to input filter

I'd like to figure out how to add this instantiated file extension validator to the input filter for my flagicon element.

Here is the input filter code:

            $inputFilter = new InputFilter();
            $this->setInputFilter($inputFilter);

            $validator = new \Zend\Validator\File\Extension(array('php'));
            $inputFilter->add([
                    'name' => 'flagicon',
                    'required' => true,
                    'filters' => [],
                    'validators'=>[
                        [$validator]
                    ]
            ]);

And Here is my Form Element Code (right from an extended Form object)

            $this->add([
                    'type'  => 'file',
                    'name' => 'flagicon',
                    'attributes' => [
                            'id' => 'flagicon',
                            'class' => 'form-control'
                    ],
                    'options' => [
                            'label' => 'Locale Flag Icon',
                    ],
            ]);

Upvotes: 0

Views: 40

Answers (2)

Garry
Garry

Reputation: 1485

You could also use array notation:

$inputFilter->add([
    'name' => 'flagicon',
    'required' => true,
    'filters' => [],
    'validators' => [
        [
            'name' => 'Extension',
            'options' => [
                'extension' => 'php',
            ]
        ]
    ]
]);

Upvotes: 0

somejkuser
somejkuser

Reputation: 9040

$validator = new \Zend\Validator\File\Extension('jpeg,jpg,png,gif');
$file = new Input('flagicon');
$file->getValidatorChain()->addValidator($validator);
$inputFilter->add($file);

Upvotes: 1

Related Questions