I’m using Zend_Validate to validate some form input (Zend Framework version is 1.8.2). For some reason, using the Zend_Filter_Input interface as described here does not work:
$data = $_POST;
$filters = array('*' => array('StringTrim'));
$validators = array('driverName' => array('NotEmpty','messages' => 'This should override the default message but does not!'));
$inputFilter = new Zend_Filter_Input($filters,$validators,$data);
$messages = $inputFilter->getMessages();
debug($messages); //show me the variable contents
Output from debug($messages):
Array
(
[driverName] => Array
(
[isEmpty] => You must give a non-empty value for field 'driverName'
)
)
No matter what I do, I cannot override that message. If I use the validator directly, i.e.:
$notEmpty = new Zend_Validate_NotEmpty();
$notEmpty->setMessage('This WILL override the default validation error message');
if (!$notEmpty->isValid($_POST['driverName'])) {
$messages = $notEmpty->getMessages();
debug($messages);
}
Output from debug($messages):
Array
(
[isEmpty] => Please enter your name
)
Bottom line. I can get validators to work, but without the benefits of the Zend_Filter_Input interface method of validation I might as well write my own validation class!
Does anyone have a clue as to why this is happening, and how to fix it?
Could it be a bug?
The
messageskey in the validator array must be passed an array of key/value pairs, where the key is the validation message constant, and the value is your custom error message. Here’s an example:However, in your case, the error message you are receiving is coming from the the
allowEmptymeta command of Zend_Filter_Input. This isn’t really a standard validator. You can set it like so:If you need a different not empty message per field, I’d recommend setting
allowEmpty => trueand adding aNotEmptyvalidator with a custom message.For your reference, the correct message key for the
NotEmptyvalidator isZend_Validate_NotEmpty::IS_EMPTY