4

I have a form element that I'm setting as required:

$this->addElement('text', 'email', array(
    'label'      => 'Email address:',
    'required'   => true
));

Since I'm setting required to true, it makes sure that it's not empty. The default error message looks like this:

"Value is required and can't be empty"

I tried setting the message on the validator, but this throws a fatal error:

$validator = $this->getElement('email')->getValidator('NotEmpty');
$validator->setMessage('Please enter your email address.');

Call to a member function setMessage() on a non-object

How can I set the message to a custom error message?

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
Andrew
  • 227,796
  • 193
  • 515
  • 708

2 Answers2

7

You need to overwrite / specify the NotEmpty-validator instead of using the default one:

$this->addElement('text', 'email', array(
    'label'      => 'Email address:',
    'required'   => true,
    'validators' => array (
       'NotEmpty' => array (
          'validator' => 'NotEmpty',
          'options' => array (
              'messages' => 'YOUR CUSTOM ERROR MESSAGE'
          )
       )
    )
));
PatrikAkerstrand
  • 45,315
  • 11
  • 79
  • 94
1
$neValidator = new Zend_Validate_NotEmpty();
$neValidator->setMessage('Please enter your email address.');

$textElement = new Zend_Form_Element_Text('email');
$textElement->addValidator($neValidator);
mak_59
  • 11
  • 1