views:

580

answers:

3

Hi all,

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?

+3  A: 

The messages key 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:

 $validators = array(

  'excerpt' => array(
   'allowEmpty' => true,
   array('StringLength', 0, Ctrl::getOption('blog/excerpt/length')), 
   'messages' => array(Zend_Validate_StringLength::TOO_LONG => 'The post excerpt must not exceed '.Ctrl::getOption('blog/excerpt/length').' characters.')
  ),

 );

However, in your case, the error message you are receiving is coming from the the allowEmpty meta command of Zend_Filter_Input. This isn't really a standard validator. You can set it like so:

$options = array(
    'notEmptyMessage' => "A non-empty value is required for field '%field%'"
);

$input = new Zend_Filter_Input($filters, $validators, $data, $options);

// alternative method:

$input = new Zend_Filter_Input($filters, $validators, $data);
$input->setOptions($options);

If you need a different not empty message per field, I'd recommend setting allowEmpty => true and adding a NotEmpty validator with a custom message.

For your reference, the correct message key for the NotEmpty validator is Zend_Validate_NotEmpty::IS_EMPTY

jason
karim79
+1  A: 

The MESSAGES argument takes an array, not a string. Try this:

$validators = array('driverName' => 
                     array('NotEmpty',
                           'messages' => array(
                                0 => 'This should override the default message but does not!'
                            )
                     )
              );
Steve Goodman
@Steve - I had already tried that, the manual would have you believe that it works, when in fact it does not, but thanks for the answer nontheless :)
karim79
@Karim - That's too bad - it used to work this way in ZF 1.0, as well as accepting validation constants as keys in the message override array. I liked how ZF provided an index based shortcut, but I guess they really want to make everything explicit/convention based.
Steve Goodman
A: 

It's a bug, it's in the JIRA of Zend Framework...

Julien