我有一个表单在我的ZF2应用程序与CAPTCHA元素如下:
$this->add(array(
'type' => 'Zend'Form'Element'Captcha',
'name' => 'captcha',
'attributes' => array(
'class'=>'form-control',
),
'options' => array(
'label' => 'Please verify you are human.',
'captcha' => array('class' => 'Dumb')
),
));
我在表单上附加了一个输入过滤器,用于验证表单中的其他元素(姓名、电子邮件、消息)。当将此附加到表单时,在检查是否有效时将忽略对CAPTCHA字段的验证。
if ($request->isPost()) {
// set the filter
$form->setInputFilter($form->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) { ...
如果我删除输入过滤器,那么CAPTCHA字段被正确验证,但显然其他字段没有验证器。我犯了什么愚蠢的错误?是否有一个"CAPTCHA"验证器,我必须在输入过滤器设置?
这个问题是因为,我假设在您的表单上您已经创建了一个方法:
getInputFilter ();
覆盖了原来的getInputFilter(),
有两个解决方案:
将表单上的函数重命名为getInputFilterCustom()
,然后修改also:
if ($request->isPost()) {
// set the filter
$form->setInputFilter($form->getInputFilterCustom());
或在当前的getInputFilter()中添加验证captcha的逻辑
这是我的代码添加一个验证码图像控制在一个ZF2形式:
$this->add(array(
'name' => 'captcha',
'type' => 'Captcha',
'attributes' => array(
'id' => 'captcha',
'autocomplete' => 'off',
'required' => 'required'
),
'options' => array(
'label' => 'Captcha :',
'captcha' => new 'Zend'Captcha'Image(array(
'font' => 'public/fonts/arial.ttf',
'imgDir' => 'public/img/captcha',
'imgUrl' => 'img/captcha'
))
),
));
其他表单元素正在使用来自输入过滤器的验证器,但我没有使用任何验证器来使其工作。
我希望这对你有帮助。
这是因为您没有调用您的父getInputFilter()。只是做
public function getInputFilter()
{
parent::getInputFilter();
//... your filters here
}