0
votes

It seems that using addErrorMessage() overrides all other validation errors.

For example, I created a custom phone element. And I also created a custom validation class that checks for a custom business rule. I expected it to print out the error messages from My_Validate_BusinessPhone when it did not meet the custom business rule. But it prints message set in addErrorMessage() all the time. Is this the normal behavior? Is there a way to chain the error messages?

$phone = new My_Form_Element_Phone( 'phone' );   
$phone->setRequired( TRUE )
    ->setAttrib( 'id', 'phone' )
    ->addErrorMessage( 'Please provide a valid phone number' )
    ->addValidator( new My_Validate_BusinessPhone );

I thank you in advance.

1

1 Answers

0
votes

The messages are overwritten, because you are setting the message to the form element and not to the validator. So that's how it should work: First, get your form element. In your case, just use it. Second, get the validator by name (I don't know how it's exacly called here, e.g. it could be 'notEmpty') and third, add your message for this validator.

$phone->getValidator('yourValidatorsName')->setMessage('Please provide a valid phone number');

I've just tested this in my own script, but I hope it should work ;-)