我想弄清楚如何将这个实例化的文件扩展名验证器添加到我的flagicon元素的输入过滤器中。
以下是输入过滤器代码:
$inputFilter = new InputFilter();
$this->setInputFilter($inputFilter);
$validator = new \Zend\Validator\File\Extension(array('php'));
$inputFilter->add([
'name' => 'flagicon',
'required' => true,
'filters' => [],
'validators'=>[
[$validator]
]
]);
这是我的表单元素代码(来自扩展的Form对象)
$this->add([
'type' => 'file',
'name' => 'flagicon',
'attributes' => [
'id' => 'flagicon',
'class' => 'form-control'
],
'options' => [
'label' => 'Locale Flag Icon',
],
]);
答案 0 :(得分:1)
$validator = new \Zend\Validator\File\Extension('jpeg,jpg,png,gif');
$file = new Input('flagicon');
$file->getValidatorChain()->addValidator($validator);
$inputFilter->add($file);
答案 1 :(得分:0)
您也可以使用数组表示法:
$inputFilter->add([
'name' => 'flagicon',
'required' => true,
'filters' => [],
'validators' => [
[
'name' => 'Extension',
'options' => [
'extension' => 'php',
]
]
]
]);