views:

546

answers:

1

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  A: 

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.

jason
Unfortunately only the setRegisterInArrayValidator(false) solution works. The getValidator('InArray') doesn't return anything, and the getValidators() method only lists my own Between validator. Thank you nevertheless!
Wabbitseason