Zend Framework2中的"IsImage validation"或"MimeType Validator"

Anj*_*K P 3 php zend-form zend-framework2

我正在使用formFilter方法(工厂方法)来验证Zend Framework 2中的表单.请有人帮助添加文件上传验证.

请指定如何在formfilter中使用" IsImage validation "或" MimeType Validator ".

小智 5

试试这个

public function getInputFilter()
{
    if (!$this->filter) {
        $this->filter = new InputFilter();
        $factory = new InputFactory();
        $this->filter->add($factory->createInput(array(
                    'name' => 'image',
                    'required' => true,
                    'validators' => array(
                        array(
                            'name' => 'NotEmpty',
                            'options' => array(
                                'messages' => array(
                                    'isEmpty' => 'Please select an icon to upload.',
                                ),
                            ),
                        ),
                        array(
                            'name' => '\Zend\Validator\File\IsImage',
                            'options' => array(
                                'messages' => array(
                                    'fileIsImageFalseType' => 'Please select a valid icon image to upload.',
                                    'fileIsImageNotDetected' => 'The icon image is missing mime encoding, please verify you have saved the image with mime encoding.',
                                ),
                            ),
                        ),
                    ),
        )));
    }
    return parent::getInputFilter();
}
Run Code Online (Sandbox Code Playgroud)

链接