验证器和错误消息

验证器和错误消息

本文介绍了Zend 验证器和错误消息:addValidator 和 addErrorMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个附加了多个验证器的表单元素(本例中为 3 个),当每个唯一验证器失败时,我将如何使用 addErrorMessage 创建自定义错误消息.有没有办法为每个验证器添加自定义消息?

If I have a form element that has multiple validators attached to it (3 in this example), how would I use addErrorMessage to create custom error messages when each unique validator fails. Is there a way to add a custom message for each validator?

$element = new Zend_Form_Element_Text()...
$element->....
        ->addValidator(...)
        ->addValidator(...)
        ->addValidator(...)
        ->addErrorMessage()

推荐答案

通常是按验证器错误消息完成,而不是按验证器...

Typically it's done per validator error message, not per validator...

$element->setErrorMessages(array(Zend_Validate_...::CONSTANT => 'New Message'));

但我通常更喜欢将一个元素的所有错误覆盖为一个

But I often prefer to override all of an element's errors to a single

$element->setErrorMessages(array('Single Error'));

或者,如果我每个验证器都需要它,这有效...

or, if I need it per validator, this works...

$validator->setMessages('string error')

应该将验证器的所有错误覆盖到单个消息中.我在任何地方都找不到这个文档,但它对我有用.因此,它可能不适用于所有版本?

should override all a validator's errors to a single message. I could not find this documented anywhere, but it works for me. Because of this, it may not work for all versions?

对我来说,错误消息处理有点混乱,除非您想覆盖所有可能的错误消息,但希望这些解决方案之一适合您.

To me, the error messaging handling is a bit messy unless you want to override every possible error message, but hopefully one of these solutions works for you.

干杯

这篇关于Zend 验证器和错误消息:addValidator 和 addErrorMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:05