如何在Zend Framework 2(ZF2)

时间:2016-06-04 06:23:04

标签: zend-framework zend-framework2 captcha

我正在使用Image Captcha,我想自定义“Captcha value is wrong”错误消息。

这是我的Captcha的定义:

$captcha = new ImageCaptcha();
$font = "./data/fonts/calibri.ttf";
$captcha->setFont($font);
$captcha->setDotNoiseLevel(55);
$captcha->setLineNoiseLevel(3);

在我的表单构造函数中:

$this->add(array(
    'type' => 'Zend\Form\Element\Captcha',
    'name' => 'captcha',
    'options' => array(
        'label' => 'Please verify you are human.',
        'captcha' => $this->captcha,
        'messages' => array(
            \Zend\Captcha\Image::BAD_CAPTCHA => 'super neat captcha message.',
        ),
    ),
));

我也尝试过:

$this->add(array(
    'type' => 'Zend\Form\Element\Captcha',
    'name' => 'captcha',
    'options' => array(
        'label' => 'Please verify you are human.',
        'captcha' => $this->captcha,
        'messages' => array(
            \Zend\Captcha\AbstractWord::BAD_CAPTCHA => 'super neat captcha message.',
        ),
    ),
));

但我不断收到“Captcha值错误”的消息。

2 个答案:

答案 0 :(得分:0)

“消息”部分必须位于验证码选项中,如下所示:

$this->add([
  'type' => 'Zend\Form\Element\Captcha',
  'name' => 'captcha',
  'options' => [
      'label' => 'Please verify you are human.',
      'captcha' => [
          ...
          'messages' => [
             \Zend\Captcha\Image::BAD_CAPTCHA => 'super neat captcha message.',
          ],
      ],   
  ],
]);

至少它适用于Zend Framework 3。

答案 1 :(得分:-1)

添加验证码元素:

   $this->add(array(
            'type' => 'Zend\Form\Element\Captcha',
            'name' => 'captcha',
            'options' => array(
                'label' => 'Please verify you are human.',
                'captcha' => $this->captcha            
            ),
        ));

错误消息:

$this->getInputFilter()->get('captcha')->setErrorMessage('super neat captcha message.');

它对我有用。