I’ve used validator errors customization example from this answer: https://stackoverflow.com/a/4881030/822947, but I need to override some of translated errors with addValidator() or setMessage()/setMessages(). Examples below doesn’t work (seems, built in translator has priority?)… How can I do it?
$field->addValidator ('Alpha', false, array ('messages' => array (Zend_Validate_Alpha::NOT_ALPHA => 'My msg')));
$Alpha = new Zend_Validate_Alpha ();
$Alpha->setDisableTranslator (true);
$Alpha->setMessage ('My msg', Zend_Validate_Alpha::NOT_ALPHA);
$Alpha->setMessages (array (Zend_Validate_Alpha::NOT_ALPHA => 'My msg'));
$field->addValidator ($Alpha);
UPDATE
The problem not in the way I add validator and set messages to it.
My goal is to localize all error messages. But for some form fields I need to add more specific messages.
For example, in my lang/translate.php I have common
Zend_Validate_Alpha::NOT_ALPHA => 'Value contains non alphabetic characters',
but for name field I need more specific
Zend_Validate_Alpha::NOT_ALPHA => 'Field can contain only alphabetic characters and spaces',
The problem is specific message ignored when I enable built in translator. So for example code
$form = new Zend_Form ();
$validator = new Zend_Validate_Alpha ();
$validator->setMessages (array (
Zend_Validate_Alpha::NOT_ALPHA => 'xxx %value% x'
));
$form->addElement ('text', 'digit', array (
'validators' => array (
$validator
)
));
$name = new Zend_Form_Element_Text ('name');
$name->addValidator ('Alpha', true, array (
'allowWhiteSpace' => true,
'messages' => array
(
Zend_Validate_Alpha::NOT_ALPHA => 'my more specific localized msg',
)
));
$form->addElement ($name);
$form->isValid (array (
'digit' => '___',
'name' => '___',
));
Zend_Debug::dump ($form->getMessages ());
when translator disabled, I have
array(2) {
'digit' =>
array(1) {
'notAlpha' =>
string(9) "xxx ___ x"
}
'name' =>
array(1) {
'notAlpha' =>
string(30) "my more specific localized msg"
}
}
when translator enabled, I have messages from lang/translate.php
array(2) {
'digit' =>
array(1) {
'notAlpha' =>
string(104) "common localized msg"
}
'name' =>
array(1) {
'notAlpha' =>
string(104) "common localized msg"
}
}
The problem is that Alpha validator does not support options array in parameters to constructor. You have to set them separatelly.
Update
The problem is with your translator then.
I have my translator configured like this:
file en.php:
and it works.