6

独自のカスタム エラー メッセージを Captcha に設定しようとしていますが、何らかの理由で 2 回エコーされます。

ここに私のキャプチャコードがあります:

$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 に答える