Zend 验证器的一条错误消息而不是几条

发布于 2024-11-08 01:27:22 字数 883 浏览 1 评论 0原文

我有下一个元素:

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

如果电子邮件地址中存在多个错误,则会显示一些错误。像这样:

'fff.fgdf' is no valid hostname for email address '[email protected]'
'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

我怎样才能只设置1条消息?我尝试过 setMessage(string),但它显​​示 3 个相同的错误。谢谢。对不起我的英语。和平与爱)

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 '[email protected]'
'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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烏雲後面有陽光 2024-11-15 01:27:22

在过去,我必须为此创建一个自定义验证器:

/**
 * 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,
    );

}

然后使用以下调用:

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

如果您只想使用与平常相同的语法:

$email->addValidator('EmailAddress');

但让它使用此验证器,那么您可以更改类名/文件名EmailAddressSimpleMessage 简单地 EmailAddress 并使用表单/元素注册前缀 App_Validate_

更好的可能是允许此类接受您想要的消息的可选构造函数参数,但当时我的做法又快又脏。

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.

爱的故事 2024-11-15 01:27:22

如果我没记错的话,您可以调用 setErrorMessages() 在表单元素上设置单个错误消息,而不是在每个单独的验证器上调用 setMessages() :

$form->addElement('password', 'password', array(
    'label' => 'New password:',
    'required' => true,
    'validators' => array(
        array('StringLength', false, 6),
        // more validators
    ),
    'errorMessages' => array('Invalid password.')
));

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.')
));
止于盛夏 2024-11-15 01:27:22

这是一个例子(我希望它有帮助):

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

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.");
|煩躁 2024-11-15 01:27:22

这是答案,通过使用 $breakChainOnFailure = true:

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

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

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

五里雾 2024-11-15 01:27:22

您使用这个例子:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文