Custom error message for Captcha element in Zend Framework 1.10

I am trying to set my own error message on my Captcha, but for some reason it echoes twice.

Here is my captcha code:

$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" ))); 

And then, to set your own error message:

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

When the check fails, I get:

 'My message here; My message here' 

Why does he duplicate the error and how to fix it?

+6
zend-framework zend-form-element
source share
2 answers

After spending a lot of time trying to get this to work, I ended up setting up messages in the constructor options

 $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' ) ) ) ); 
+14
source share

I looked at this answer, but I didn’t like it, now I did it using the input specification:

 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; } 

I just noticed it was a question for ZF1

This is the answer for ZF2

+1
source share

All Articles