Zend_Validate_Between 奇怪的错误信息 [英] Zend_Validate_Between strange error message

查看:36
本文介绍了Zend_Validate_Between 奇怪的错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验 Zend_Validate_Between 类.

我是这样设置的:

$scoreBetweenValidator = new Zend_Validate_Between(-3, 3, true);

所以验证器应该只接受 -3 到 3 之间的值,包括这两个值.

so the validator should only accept values between -3 and 3, inclusive.

在一个无效值上,我得到一个 '%value%' is not found in the haystack 错误消息,我认为它属于 Zend_Validate_InArray 类 (Zend_Validate_InArray::NOT_IN_ARRAY).

On an invalid value I got a '%value%' was not found in the haystack error message, which I think belongs to the Zend_Validate_InArray class (Zend_Validate_InArray::NOT_IN_ARRAY).

我的问题是我希望通过 setMessages 方法使用自定义错误消息,但我不知道如何为这个看似外来的消息键设置它.

My problem is that I wish to use custom error messages with the setMessages method, but I don't know how I could set it up for this seemingly foreign message key.

我试过了:

$scoreBetweenValidator->setMessages(array(
        Zend_Validate_Between::NOT_BETWEEN_STRICT => 'my custom msg',
        Zend_Validate_Between::NOT_BETWEEN => 'my other custom msg',
            //'notInArray' => "doesn't work"
            //Zend_Validate_InArray::NOT_IN_ARRAY => "also doesn't work"
    ));

但我得到一个不存在用于关键notInArray"的消息模板 异常.

在 Zend Framework 中设置自定义验证消息的首选解决方案是什么?

What is the preferred solution for setting custom validation messages in Zend Framework?

作为对杰森的回复:

Zend_Form_Element_Select 位于带有 addElements 方法的 Zend_Form 类中.

A Zend_Form_Element_Select is inside a Zend_Form class attached with addElements method.

表单没有任何其他元素,只有这个元素,也没有任何其他验证器,只有Between.

The form doesn't have any other elements just this one, and it doesn't have any other validators, just the Between.

默认情况下,选择的选项都是有效的,但是当我调整选项值(使用 Firebug)并设置无效值(作为自我黑客尝试)时,我收到了 notInArray 异常.

The select's options are all valid by default, but when I tweak the option value (with Firebug) and set an invalid value (as a self-hacking attempt) I receive the notInArray exception.

推荐答案

Zend_Form_Element_Select 自动向自身添加一个 InArray 验证器.

Zend_Form_Element_Select automatically adds an InArray validator to itself.

要为其设置错误消息,这应该可以解决问题:

To set the error message for it, this should do the trick:

$element->getValidator('InArray')->setMessage('Your inArray error message here', Zend_Validate_InArray::NOT_IN_ARRAY);

如果您根本不需要 InArray 验证器,您可以通过在元素上调用 setRegisterInArrayValidator(false) 或将 false 传递给 registerInArrayValidator 来禁用此行为> 元素创建的配置键.

If you do not want the InArray validator at all, you can disable this behavior by either calling setRegisterInArrayValidator(false) on the element, or by passing false to the registerInArrayValidator configuration key on element creation.

这篇关于Zend_Validate_Between 奇怪的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆