4

I'm new to ZF and I'm discovering how to use Zend_Form and utilize it's capability like validating and filtering input values. I already know the basic of Zend_form like building a form and add element into it. My problem is that I want to add a custom error message to form element and I want to define that message inside the action controller that instantiated the form. I want to defined the error message inside the controller because I need to perform a validation against the database. For example checking if the username/email already exist in the database. I tried googling and that's leads me to setErrorMessage method of zend_form but when I try to use it, the error message is not showing at all... I also tried zend_form->setError and still no error displaying in the view script. Is my idea of setting custom error in the action controller correct or this should be done the other way?

2 Answers 2

2

Are you using Zend_Validate_Db_RecordExists?

Something like this should work:

 $form->getElement('username')->getValidator("RecordExists")->setMessage('This username exists',
                Zend_Validate_Db_RecordExists::ERROR_RECORD_FOUND
            );
Sign up to request clarification or add additional context in comments.

3 Comments

Not sure what the OP had in mind, but my problem is that I don't have a specific validator that I can tie the error message to.
What are you trying to validate?
Custom, server-side validation. It doesn't match any existing validator. Instead of writing my own validator, I was just looking for a way to add arbitrary error messages to a form.
1

This works for me:

$form->getElement('username')->addError('This username exists.');

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.