6

我正在尝试将我自己的自定义错误消息设置到我的验证码上,但由于某种原因,它会回显两次。

这是我的验证码:

$captcha = new Zend_Form_Element_Captcha(
  'captcha', // This is the name of the input field
  array('captcha' => array(
      // First the type...
      'captcha' => 'Image',
      // Length of the word...
      'wordLen' => 6,
      // Captcha timeout, 5 mins
      'timeout' => 300,
      // What font to use...
      'font' => 'images/captcha/font/arial.ttf',
      // URL to the images
      'imgUrl' => '/images/captcha',
      //alt tag to keep SEO guys happy
      'imgAlt' => "Captcha Image - Please verify you're human"
  )));

然后设置我自己的错误信息:

$captcha->setErrorMessages(array('badCaptcha' => 'My message here'));

当验证失败时,我得到:

'My message here; My message here'

为什么它会重复错误,我该如何解决?

4

2 回答 2

14

在花了很多时间试图让它工作之后,我最终在构造函数的选项中设置了消息

$captcha = new Zend_Form_Element_Captcha(
  'captcha', // This is the name of the input field
  array(
    'captcha' => array(
      // First the type...
      'captcha' => 'Image',
      // Length of the word...
      'wordLen' => 6,
      // Captcha timeout, 5 mins
      'timeout' => 300,
      // What font to use...
      'font' => 'images/captcha/font/arial.ttf',
      // URL to the images
      'imgUrl' => '/images/captcha',
      //alt tag to keep SEO guys happy
      'imgAlt' => "Captcha Image - Please verify you're human",
      //error message
      'messages' => array(
        'badCaptcha' => 'You have entered an invalid value for the captcha'
      )
    )
  )
);
于 2011-03-09T09:18:47.813 回答
1

我调查了这个答案,但我真的不喜欢这个解决方案,现在我使用如下输入规范完成了它:

public function getInputSpecification()
{
    $spec = parent::getInputSpecification();

    if (isset($spec['validators']) && $spec['validators'][0] instanceof ReCaptcha) {
        /** @var ReCaptcha $validator */
        $validator = $spec['validators'][0];
        $validator->setMessages(array(
            ReCaptcha::MISSING_VALUE => 'Missing captcha fields',
            ReCaptcha::ERR_CAPTCHA => 'Failed to validate captcha',
            ReCaptcha::BAD_CAPTCHA => 'Failed to validate captcha', //this is my custom error message
        ));
    }

    return $spec;
}

我刚刚注意到,这是ZF1的问题

这是ZF2的答案

于 2014-08-05T00:53:51.970 回答