Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Validate_Between strange error message

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.

like image 887
Wabbitseason Avatar asked Jun 13 '09 16:06

Wabbitseason


1 Answers

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.

like image 62
jason Avatar answered Oct 06 '22 03:10

jason