Zend form showing all errors / Zend form show error above each input field
For a form I'm using the following code:
class Application_Form_User_Register extends Zend_Form
{
public function init()
{
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email adres: (*)')
->setRequired(true)
->addErrorMessage('Het veld Email adres is verplicht')
->addValidator('StringLength', false,array(6,100))
->addErrorMessage('Het email adres dient uit minstens 6 karakters te bestaan')
->addValidator('EmailAddress')
->addErrorMessage('Het veld Email adre开发者_如何学Gos moet een geldig email adres bevatten')
->addFilter('StringTrim');
$this->addElement($email);
}
}
When i post a wrong email address from this form, it shows all 3 error messages.
For example: if I post "thisisa@nonvalidadress", it shows all errors including the error "The email adres should be at least 6 characters long".
Is there a way to change this behaviour?
Then a second question... Is there a way to show the error message above the corresponding input field? Default the error message is shown underneath.
First qustion was answered. Second one should be somehow along this line:
$element->getDecorator('Errors')->setPlacement('prepend');
The second parameter of the addValidator() method is the breakChainOnFailure metacommand. Just set if to true. So you would do
class Application_Form_User_Register extends Zend_Form
{
public function init()
{
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email adres: (*)')
->setRequired(true)
//->addErrorMessage('Het veld Email adres is verplicht')
->addValidator('StringLength', true, array(6,100))
//->addErrorMessage('Het email adres dient uit minstens 6 karakters te bestaan')
->addValidator('EmailAddress', true)
//->addErrorMessage('Het veld Email adres moet een geldig email adres bevatten')
->addFilter('StringTrim');
$this->addElement($email);
}
}
This would break the validator chain if it failed.
Here is what the Zend Framework website says about custom validator messages:
Note: Providing Custom Validator Error Messages Some developers may wish to provide custom error messages for a validator. The $options argument of the Zend_Form_Element::addValidator() method allows you to do so by providing the key 'messages' and mapping it to an array of key/value pairs for setting the message templates. You will need to know the error codes of the various validation error types for the particular validator. A better option is to use a Zend_Translate_Adapter with your form. Error codes are automatically passed to the adapter by the default Errors decorator; you can then specify your own error message strings by setting up translations for the various error codes of your validators.
See this for details: http://framework.zend.com/manual/en/zend.form.elements.html
Your second question, changing where the error message shows up, requires customizing the decorators on those form elements. You can definitely do it, and it's not as difficult as it seems at first. This article really helped me understand decorators: http://devzone.zend.com/article/3450
Sudol is right about passing breakChainOnFailure
parameter in case you want to stop validations after the first failure.
But your problem is in addErrorMessage()
method which you call on the element. This method sets error messages that would be displayed in case element validation fails BUT it doesn't specify any connection between the message and the validator which failed. That's why you're getting all three messages each time any of your validators fails.
So what you need to do is to specify error messages for each validator instead of the element.
$emailValidator = new Zend_Validate_EmailAddress();
$emailValidator->setMessage( 'Het veld Email adres moet een geldig email adres bevatten' );
$email->addValidator( $emailValidator );
Alternatively, you might want to use Zend_Translate for translating standard error messages.
精彩评论