0

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?

2
  • Are you really mixing filter and validate? They have quite different purpouses... Commented Jul 15, 2009 at 18:07
  • And you're using Zend_Filter_Input::MESSAGES not validate messages - that might be the problem. Commented Jul 15, 2009 at 18:07

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you get the default message for a missing field or the default message of the NotEmpty-Validator?
I changed the answer after some research on the topic.
1

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

Comments

0

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.