One error message instead of few for Zend validato

2019-04-02 08:45发布

I have the next element:

$email = new Zend_Form_Element_Text('email');
$email->setAttribs(array('class' => 'input-text', 'id' => 'email'))
        ->setLabel($this->view->translate('Email'))
        ->setValue(null)
        ->setRequired(true)
        ->addValidator(new Zend_Validate_EmailAddress())
        ->setDecorators($emailMessageDecorators);

If there are more than one mistake in the email address, some errors are displaying. Like this:

'fff.fgdf' is no valid hostname for email address 'as@fff.fgdf'
'fff.fgdf' appears to be a DNS hostname but cannot match TLD against known list
'fff.fgdf' appears to be a local network name but local network names are not allowed

How can I set only 1 message? I have tryed setMessage(string), but it shows 3 same errors. Thanks. Sorry for my english. Peace & love)

5条回答
叼着烟拽天下
2楼-- · 2019-04-02 09:10

In the past, I had to make a custom validator for that:

/**
 * The standard email address validator with a single, simple message
 */
class App_Validate_EmailAddressSimpleMessage extends Zend_Validate_EmailAddress
{
    const COMMON_MESSAGE = 'Invalid email address';

    protected $_messageTemplates = array(
        self::INVALID            => self::COMMON_MESSAGE,
        self::INVALID_FORMAT     => self::COMMON_MESSAGE,
        self::INVALID_HOSTNAME   => self::COMMON_MESSAGE,
        self::INVALID_MX_RECORD  => self::COMMON_MESSAGE,
        self::INVALID_SEGMENT    => self::COMMON_MESSAGE,
        self::DOT_ATOM           => self::COMMON_MESSAGE,
        self::QUOTED_STRING      => self::COMMON_MESSAGE,
        self::INVALID_LOCAL_PART => self::COMMON_MESSAGE,
        self::LENGTH_EXCEEDED    => self::COMMON_MESSAGE,
    );

}

Then called using:

$email->addValidator(new App_Validate_EmailAddressSimpleMessage());

If you just want to use the same syntax as usual:

$email->addValidator('EmailAddress');

but have it use this validator, then you can change the classname/filename from EmailAddressSimpleMessage to simply EmailAddress and register the prefix App_Validate_ with the form/elements.

Even better would probably be to allow this class to accept an optional constructor parameter for the message you want, but I was going quick-and-dirty at the time.

查看更多
姐就是有狂的资本
3楼-- · 2019-04-02 09:12

You using this example:

$email_validator = new Zend_Validate_EmailAddress();
$email = new Zend_Form_Element_Text('email');
$email->setRequired('true')
      ->setLabel('Email: ')
      ->setDecorators(array(array('ViewHelper'), array('Errors')))
      ->addValidator($email_validator);
查看更多
贼婆χ
4楼-- · 2019-04-02 09:17

This is an example(I hope it helps) :

$email = new Zend_Form_Element_Text('emailid');

$email->setLabel("Email-Adress :* ")
      ->setOptions(array('size' => 20))
      ->setRequired(true)
      ->addFilter('StripTags')
      ->addFilter('StringTrim')
      ->addValidator('EmailAddress')
      ->getValidator('EmailAddress')->setMessage("Please enter a valid e-mail address.");
查看更多
你好瞎i
5楼-- · 2019-04-02 09:29

If I recall correctly, you can call setErrorMessages() to set a single error message on the form element, rather than calling setMessages() on each individual validator:

$form->addElement('password', 'password', array(
    'label' => 'New password:',
    'required' => true,
    'validators' => array(
        array('StringLength', false, 6),
        // more validators
    ),
    'errorMessages' => array('Invalid password.')
));
查看更多
forever°为你锁心
6楼-- · 2019-04-02 09:32

Here is the answer, by using $breakChainOnFailure = true:

addValidator($nameOrValidator, $breakChainOnFailure = false, array $options = null);

Cited: http://framework.zend.com/manual/1.12/en/zend.form.elements.html#zend.form.elements.validators.errors

查看更多
登录 后发表回答