ZF2 验证码验证始终有效
ZF2 Captcha validation always is Valid
我在表单中添加了验证码
我的表格是用 class
的方法创建的
这是方法定义
public function generateFormAndValidate($urlcaptcha = null)
{
$form = new \Zend\Form\Form();
$formInputFilter = $form->getInputFilter();
$inputFactory = new \Zend\InputFilter\Factory();
$dirdata = './data';
//pass captcha image options
$captchaImage = new CaptchaImage( array(
'font' => $dirdata . '/fonts/arial.ttf',
'width' => 250,
'height' => 100,
'dotNoiseLevel' => 40,
'lineNoiseLevel' => 3)
);
$captchaImage->setImgDir($dirdata.'/captcha');
$captchaImage->setImgUrl($urlcaptcha);
//add captcha element...
$form->add(array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human',
'captcha' => $captchaImage,
),
));
$formInputFilter->add ( $inputFactory->createInput ( array(
'name' => 'captcha',
// 'required' => 'true'
))
);
}
在控制器中:
// ......
$form->setData($post);
if ($form->isValid()) {
}
else {
}
//....
我的验证码图像正确显示,验证码已发布,我检查了会话,一切正常
但 $form->isValid
return 任何时候都是正确的,甚至我将验证码输入为空或者使用任何字符 returns True
anyway
我想 isValid
方法根本没有检查验证码。
在您的控制器中:
//...
$form->setData($post);
$form->setInputFilter($form->getInputFilter()); //You missed this line
if ($form->isValid()) {
// ...
我在表单中添加了验证码
我的表格是用 class
这是方法定义
public function generateFormAndValidate($urlcaptcha = null)
{
$form = new \Zend\Form\Form();
$formInputFilter = $form->getInputFilter();
$inputFactory = new \Zend\InputFilter\Factory();
$dirdata = './data';
//pass captcha image options
$captchaImage = new CaptchaImage( array(
'font' => $dirdata . '/fonts/arial.ttf',
'width' => 250,
'height' => 100,
'dotNoiseLevel' => 40,
'lineNoiseLevel' => 3)
);
$captchaImage->setImgDir($dirdata.'/captcha');
$captchaImage->setImgUrl($urlcaptcha);
//add captcha element...
$form->add(array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human',
'captcha' => $captchaImage,
),
));
$formInputFilter->add ( $inputFactory->createInput ( array(
'name' => 'captcha',
// 'required' => 'true'
))
);
}
在控制器中:
// ......
$form->setData($post);
if ($form->isValid()) {
}
else {
}
//....
我的验证码图像正确显示,验证码已发布,我检查了会话,一切正常
但 $form->isValid
return 任何时候都是正确的,甚至我将验证码输入为空或者使用任何字符 returns True
anyway
我想 isValid
方法根本没有检查验证码。
在您的控制器中:
//...
$form->setData($post);
$form->setInputFilter($form->getInputFilter()); //You missed this line
if ($form->isValid()) {
// ...