Hello!
I have a signup form that is working fine. Here it is a text element for providing the email address:

$email = new Zend_Form_Element_Text('email');
        $email->setLabel($sessionSettings->tr->translate('email'))
              ->setValue('')
              ->setAttribs(array('id' => 'email', 'class' => 'required email'))
              ->setRequired(true)
              ->addFilters(array('StripTags','StringTrim','StringToLower'))
              ->addValidators(array(
                  'NotEmpty',
                  'EmailAddress',
                  array('Db_NoRecordExists', true, array('user', 'email_address'))
              ));
        $email->getValidator('NotEmpty')->setMessage('Email address field cannot be empty.');
        $email->getValidator('EmailAddress')->setMessage('Email address name not corresponding.');
        $email->getValidator('Db_NoRecordExists')->setMessage('Email address is taken.');

I wonder if it is possible to add the error messages to 'addValidators' array and removing in this way the last 3 lines. Please advise.

What ZF version are you using. In the 1.X manual there's a setMessages method, which should enable you to set them all at once. I searched the 2.X manual, but haven't found it yet.

@pritaeas

Thank you for fast reply. I am using version 1.X. I have tried the method setMessages and didn't work. Then I tried other method setErrorMessages and it worked.

->addValidators(array(
                  'NotEmpty',
                  'EmailAddress',
                  array('Db_NoRecordExists', true, array('user', 'email_address'))
              ))->setErrorMessages(array('Email address is taken.'));

What I was looking for is this:

$email = new Zend_Form_Element_Text('email');
        $email->setLabel($sessionSettings->tr->translate('email'))
              ->setValue('')
              ->setAttribs(array('id' => 'email', 'class' => 'required email'))
              ->setRequired(true)
              ->addFilters(array('StripTags','StringTrim','StringToLower'))
              ->addValidators(array(
                  array('NotEmpty',true, array('messages' => array('isEmpty' => 'Email address field cannot be empty.'))),
                  array('EmailAddress',true, array('messages' => array('emailAddressInvalidFormat' => 'Email address name not corresponding.'))),
                  array('Db_NoRecordExists', true, array('table' => 'user', 'field' => 'email_address', 'messages' => array('recordFound' => 'Email address is taken.')))
              ));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.