0

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
        )

    [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.

Charles
  • 50,943
  • 13
  • 104
  • 142
Centurion
  • 5,169
  • 6
  • 28
  • 47

2 Answers2

1

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.

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • The problem is that i am not using Zend_Form, i just need simple form validation, thanks. – Centurion Apr 03 '11 at 16:22
  • Why not? Zend_Form makes it simple dealing with forms. However in your code you seem to setup only one validator, but you need two. Try to seperate them. You could try static validator, Zend_Validate::is($email, 'EmailAddress') – DarkLeafyGreen Apr 04 '11 at 05:01
0
$validator = new Zend_Validate_EmailAddress();
$validator->setMessage(
    'A valid email is required',
    Zend_Validate_EmailAddress::INVALID
);
SMka
  • 3,021
  • 18
  • 14