ufk
ufk

Reputation: 32134

IsImage validator in ZF2 forms

I'm having problems implementing the IsImage File validator in a Form class in Zend Framework 2-beta5.

In general I'm having problems using any File validator in a Zend Form class in Zend framework 2.

I couldn't find any relevant documentation.

I found that for example Float validator that resides at Library/Zend/Validator can be used with the following code:

    $this->setInputFilter($inputFactory->createInputFilter(array(
            'alcohol_vol' =>     array(
                    'name' => 'alcohol_vol',
                    'required' => true,
                    'validators' => array(
                            array(
                                    'name' => 'Float',
                            ),
                    ),
            ),
    ))); 

the IsImage file validator resides at /Library/Zend/Validator/File and can't find a way to use it. any information regarding the issue would be greatly appreciated.

thanks!

Kfir

Upvotes: 1

Views: 1116

Answers (2)

Tomasz Racia
Tomasz Racia

Reputation: 1806

Try to add this snippet under validators key, like this:

'validators' => array(
    array(
        'name' => '\Zend\Validator\File\IsImage',
        'options' => array(
            'break_chain_on_failure' => true,
        ),
    ),
),

But sometimes, depends on server configuration, IsImage might not working. Then use Extension validator instead:

'validators' => array(
    array(
        'name' => '\Zend\Validator\File\Extension',
        'options' => array(
            'extension' => array(
                'png', 'jpeg', 'jpg',
            ),
            'break_chain_on_failure' => true,
        ),
    ),
),

Upvotes: 1

AlloVince
AlloVince

Reputation: 1655

Upload file validate/filter should use Zend\File\Transfer but not Zend\Form

Try below way to add file validator

$fileTransfer = new Zend\File\Transfer\Transfer();
$fileTransfer->addValidators(array(
    array('IsImage', true)
));
if($fileTransfer->isValid()){
    $fileTransfer->receive();
}

Upvotes: 0

Related Questions