Custom Error Message for Captcha Element In Zend Framework 1.10
Asked Answered
C

2

6

I am trying to set my own custom error message onto my Captcha but for some reason it is echoing 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 my own error message:

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

When the validation fails I get:

'My message here; My message here'

Why is it duplicating the error and how do I fix it?

Corncrib answered 22/2, 2011 at 9:13 Comment(2)
there is a possibility that the error setter is called twice. you should scan the code.Twoseater
@Twoseater - setErrorMessages clears previous error messages and adds this one. As far as I can tell it is failing validation twice, so it is adding the error message twice. Since I'm using image, as far as I can tell, it is creating a (hidden) id field and the text input field. According to the documentation they are both validated as one element, but I'm having doubts as it's the only thing I can think of that explains my duplicitous error message problemCorncrib
C
14

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

$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'
      )
    )
  )
);
Corncrib answered 9/3, 2011 at 9:18 Comment(0)
K
1

I looked into this answer, but I didn't really like this solution, Now I have done it using an inputspecification like:

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, this was a question for ZF1

This is the answer for ZF2

Kassie answered 5/8, 2014 at 0:53 Comment(1)
+1 - With the release of ZF2 I'm sure this page will start attracting traffic for that. This answer will hopefully help others :) That said, you may want to ask and answer a question of the above specific to ZF2?Corncrib

© 2022 - 2024 — McMap. All rights reserved.