如何在 Zend Framework 2 (ZF2) 中自定义 BAD CAPTCHA 消息



>我正在使用图像验证码,我想自定义"验证码值错误"错误消息。

这是我的验证码的定义:

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

在我的窗体的构造函数中:

$this->add(array(
    'type' => 'ZendFormElementCaptcha',
    'name' => 'captcha',
    'options' => array(
        'label' => 'Please verify you are human.',
        'captcha' => $this->captcha,
        'messages' => array(
            ZendCaptchaImage::BAD_CAPTCHA => 'super neat captcha message.',
        ),
    ),
));

我也试过:

$this->add(array(
    'type' => 'ZendFormElementCaptcha',
    'name' => 'captcha',
    'options' => array(
        'label' => 'Please verify you are human.',
        'captcha' => $this->captcha,
        'messages' => array(
            ZendCaptchaAbstractWord::BAD_CAPTCHA => 'super neat captcha message.',
        ),
    ),
));

但是我不断收到"验证码值错误"的消息。

"消息"部分必须在验证码选项中,如下所示:

$this->add([
  'type' => 'ZendFormElementCaptcha',
  'name' => 'captcha',
  'options' => [
      'label' => 'Please verify you are human.',
      'captcha' => [
          ...
          'messages' => [
             ZendCaptchaImage::BAD_CAPTCHA => 'super neat captcha message.',
          ],
      ],   
  ],
]);

至少它在Zend Framework 3上对我有用。

添加验证

码元素:

   $this->add(array(
            'type' => 'ZendFormElementCaptcha',
            'name' => 'captcha',
            'options' => array(
                'label' => 'Please verify you are human.',
                'captcha' => $this->captcha            
            ),
        ));

和错误消息:

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

它对我有用。

最新更新