Tag Archives: Numbers

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!

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