我正在尝试将自己的自定义错误消息设置到我的Captcha上,但由于某种原因,它会回显两次。
这是我的验证码:
$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'
为什么要重复错误以及如何解决?
答案 0 :(得分: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'
)
)
)
);
答案 1 :(得分: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的答案