How to Setup Different Error Messages for Each Zend Form Element Validator

Anyone who has worked with that has come across this problem. I’d like to show different error message on each validator attached to a Zend_Form_Element. Let’s say we validate an text input field. We want it to contain only digits, but also we’d like to display different messages when the field is empty and when the user has entered something that is different from digits.

It can be done by attaching to the form element two validators: Zend_Validate_Digits and Zend_Validate_NotEmpty, but first let’s see how to change the default “Value is required and can’t be empty” error message of a form field.

$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('Digits');
$form->addElement($element);

Here we validate the field with Zend_Validate_Digits and we have set it to be required. Thus everything containing characters, i.e. “my123name” or “007bond”, will be false, while “1234” will be true.

Zend Framework
To show different error messages you've to attach them per validator and not per form element!


First of all this field is set to be required with the line ->setRequired(true), so we cannot submit the form if the input is empty and we’ll receive the default error message “Value is required and can’t be empty”. The question is how to change this default message, because as you know sometimes you’d like to say something different to your users or you’d like to display error messages on a language different from English. Here’s how.

$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('Digits')
	->addErrorMessage('Please, type your phone here!');
$form->addElement($element);

Now the error message is changed from “Value is required and can’t be empty” to “Please, type your phone here!”.

The problem is that when you add more than one validator to a form field you can still show one message regardless of the validator that has failed.

$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('NotEmpty', true)
	->addValidator('Digits', true)
	->addErrorMessage('Please, type your phone here!');
$form->addElement($element);

In this case whenever the field is empty or it contains something different from digits, the message shown to the user will be “Please, type your phone here!”. The question is can we show different error messages on every validator. This should look something like “The field cannot be empty!” when the field is empty and “Please, enter only digits!” when the user has entered something into the field, but it doesn’t contain only digits.

The Solution

Actually we have to attach error messages per validator, and not on a form element. Here’s how it can be done.

$notEmpty = new Zend_Validate_NotEmpty();
$notEmpty->setMessage('The field cannot be empty!');
 
$digits = new Zend_Validate_Digits();
$digits->setMessage('Please, enter only digits');
 
$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone:')
	->setRequired(true)
	->addValidator($notEmpty, true)
	->addValidator($digits, true);
$form->addElement($element);

Note that we set to “true” the second parameter of addValidator. This is important because this way we break the validator’s chain and when the validation fails on NotEmpty the framework stops the validation of that field against the other validators.

6 thoughts on “How to Setup Different Error Messages for Each Zend Form Element Validator

  1. its not working, you can only pass array as argument not object.
    and addValidator() doesnt exist. its addValidators()

  2. ok got it working ..
    you have add validator separately like this

    $notEmpty = new Zend_Validate_NotEmpty();
    $notEmpty->setMessage(‘Field can not be empty’);

    $emailValidate = new Zend_Validate_EmailAddress();
    $emailValidate->setMessage(’email is not valid’);

    $email = $this->createElement(‘text’, ’email’);
    $email->setLabel(‘Username’)
    ->setRequired(TRUE);

    $email->addValidator($notEmpty, TRUE);
    $email->addValidator($emailValidate, TRUE);

Leave a Reply

Your email address will not be published. Required fields are marked *