0
votes

I’m using Zend_Filter_Input to validate form data and want to customize the error messages, if a user does not enter a value. It is important that each field gets a different error message.

With Zend Framework 1.8.0 I used the following array for the “validator” parameter of Zend_Filter_Input:

$validators = array(
    'salutation' => array(
        new Zend_Validate_NotEmpty(),
        Zend_Filter_Input::MESSAGES => array(
            Zend_Validate_NotEmpty::IS_EMPTY => "Please enter a salutation"
        )
    ),
    /* ... */
);

Since I’ve upgraded to ZF 1.8.4, I always get the default message for empty fields (“You must give a non-empty value for field '%field%'”). Obviously Zend_Filter_Input does not call the Zend_Validate_NotEmpty validator anymore, if the field is empty.

Is there a way to change this behavior or another way to get customized “empty” messages for each field?

3
Are you really mixing filter and validate? They have quite different purpouses...Tomáš Fejfar
And you're using Zend_Filter_Input::MESSAGES not validate messages - that might be the problem.Tomáš Fejfar

3 Answers

1
votes

It seems that Zend_Filter_Input changed its behaviour when handling empty fields. Empty fields are never processed by rule validators. If a field is empty and allowEmpty is set to true, none of your validators are used. If the field is empty and allowEmpty is set to false, then the default message for empty values is set. Currently there is no way to customize this message for a specific field.

1
votes

The behavior has not changed. This is a bug (http://framework.zend.com/issues/browse/ZF-7394)

0
votes

try this:

    $validators = array(
        'salutation' => array('NotEmpty', Zend_Filter_Input::MESSAGES => 'Please enter a salutation')
        );

I don't know why, but seems they changed the constant "isEmpty" with "NotEmpty" (without including it in the Zend_Validate_NotEmpty class). Sometimes I just go nuts with Zend. :)