开发者

One error message instead of few for Zend validator

开发者 https://www.devze.com 2023-03-06 19:25 出处:网络
I have the next element: $email = new Zend_Form_Element_Text(\'email\'); $email->setAttribs(array(\'class\' => \'input-text\', \'id\' => \'email\'))

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'
开发者_StackOverflow中文版'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)


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.


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.')
));


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.");


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


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);
0

精彩评论

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