Настраиваемое сообщение об ошибке для элемента Captcha в Zend Framework 1.10 - PullRequest
6 голосов
/ 22 февраля 2011

Я пытаюсь установить свое собственное сообщение об ошибке на мою капчу, но по какой-то причине оно повторяется дважды.

Вот мой код капчи:

$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'

Почему это дублирует ошибку и как ее исправить?

Ответы [ 2 ]

14 голосов
/ 09 марта 2011

Потратив МНОГО времени на попытки заставить это работать, я закончил настройкой сообщений в опциях конструктора

$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 голос
/ 05 августа 2014

Я посмотрел на этот ответ, но мне не очень понравилось это решение. Теперь я сделал это, используя спецификацию ввода, например:

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

...