ZF 1.11.2
I've tried most of the syntaxes. They didn't click.
$validators = array('product_name' => array('alnum'));
//...
$input = new Zend_Filter_Input($filters, $validators, $_POST);
How in the world do you set a custom error message for alnum
with the syntax above? Using 'messages' => array('Not alnum!!')
? Yeah, well... How? I must've tried 100 nested arrays.
Use setMessage and disable translator if you have one.
If you use your validator on a form element, you have to disable the translator on the element.
Disabling the translator on the element will disable the translation of all the validator messages. It is not possible to use a translator on the form or element and overwrite just one validator message. When the element is validated the translator is injected to every validator. The validator will use the translator if it is set. Thereby the custom error message won't be used.
Zend_Validate_Abstract::_createMessage()
I think it is only possible to use a custom error message by disable the translator on the element.
Use the built in translator.
For example, configure the translator in your config file to use a simple array
This tells the Translate application resource plugin that you want to
APPLICATION_PATH/lang
Now, create folders for any languages you want to support. At a minimum, you'll want
application/lang/en
. For exampleIn each language folder, create a
translate.php
file. This file will contain (and return) an array of key / value pairs for each translation. You can find the keys for each validator message in the validator class. Here's an example for the Alnum validatorFor all Zend validators, you can also use the
%value%
placeholder in your message, egIf you are simply trying to change the validation messages for a form element, I have always done it like this (inside a class that extends Zend_Form):
Are you saying that this didn't work? Or are you using your validator in a more general context, in which case @Phil Brown's (awesome!) answer will do the job.