开发者

Zend Framework input validation messages

I have the following code

$validators = array(
        'name'  => array('NotEmpty',
                        'messages' => 'A valid name is required'
        ),

        'email'=> array(
            new Zend_Validate_Regex("^[a-z0-9]+[a-z0-9_-]*(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+) *\.([a-z]+){2,}$^"),
            'messages' => array('A valid email is required',
                Zend_Validate_Regex::NOT_MATCH=>'The email is not valid',)
        ))

and when i check if isValid and both name and email are empty i get for both 'A valid name is required'

the result looks like

Array
(
    [name] => Array
        (
            [isEmpty] => A valid name is required
        )

  开发者_如何学C  [email] => Array
        (
            [isEmpty] => A valid name is required
        )

)

So my question is how to make to get for each the needed message in case when both are empty? I also need the email validation to work and display the proper message. Thanks in advance.


For proper email validation you can use the built-in email validator. So when you create your form elements you can specify the validator:

$email = new Zend_Form_Element_Text('email');
$email->setDecorators($this->elementDecorators)
      ->setLabel('Email')
      ->setRequired(true)
      ->addValidator('EmailAddress')
      ->setFilters(array('StringTrim','StringToLower'));

You also can specifiy error messages:

$email->setErrorMessages(array(
      'err1' => 'Error1',
      'err2' => 'Error2'
));  

Zend_Form will then echo the proper error messages for you.


$validator = new Zend_Validate_EmailAddress();
$validator->setMessage(
    'A valid email is required',
    Zend_Validate_EmailAddress::INVALID
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜