Zend_Validate_Between strange error message

喜欢而已 提交于 2019-12-22 09:11:07

问题


I am experimenting with the Zend_Validate_Between class.

I set it up thusly:

$scoreBetweenValidator = new Zend_Validate_Between(-3, 3, true);

so the validator should only accept values between -3 and 3, inclusive.

On an invalid value I got a '%value%' was not found in the haystack error message, which I think belongs to the Zend_Validate_InArray class (Zend_Validate_InArray::NOT_IN_ARRAY).

My problem is that I wish to use custom error messages with the setMessages method, but I don't know how I could set it up for this seemingly foreign message key.

I tried this:

$scoreBetweenValidator->setMessages(array(
        Zend_Validate_Between::NOT_BETWEEN_STRICT => 'my custom msg',
        Zend_Validate_Between::NOT_BETWEEN => 'my other custom msg',
            //'notInArray' => "doesn't work"
            //Zend_Validate_InArray::NOT_IN_ARRAY => "also doesn't work"
    ));

but I got a No message template exists for key 'notInArray' exception.

What is the preferred solution for setting custom validation messages in Zend Framework?

As a reply to Jason:

A Zend_Form_Element_Select is inside a Zend_Form class attached with addElements method.

The form doesn't have any other elements just this one, and it doesn't have any other validators, just the Between.

The select's options are all valid by default, but when I tweak the option value (with Firebug) and set an invalid value (as a self-hacking attempt) I receive the notInArray exception.


回答1:


Zend_Form_Element_Select automatically adds an InArray validator to itself.

To set the error message for it, this should do the trick:

$element->getValidator('InArray')->setMessage('Your inArray error message here', Zend_Validate_InArray::NOT_IN_ARRAY);

If you do not want the InArray validator at all, you can disable this behavior by either calling setRegisterInArrayValidator(false) on the element, or by passing false to the registerInArrayValidator configuration key on element creation.




回答2:


I am a newbee so I changed messages in Zend\Validate\EmailAddress.php. Afterall its email



来源:https://stackoverflow.com/questions/991001/zend-validate-between-strange-error-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!