如何根据另一个字段的值验证Zend_Form的字段? [英] How to validate a field of Zend_Form based on the value of another field?

查看:111
本文介绍了如何根据另一个字段的值验证Zend_Form的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向字段添加自定义验证器.它应该考虑另一个字段的值.例如.字段A最多应为B + 50%.

I'm trying to add a custom validator to a field. It should take into account the value of another field. E.g. field A should be at most B+50%.

我制作了一个实现Zend_Validate_Interface的类,但是显然Zend Form仅将当前字段的值发送到验证器.如何使验证器接收所有内容?

I've made a class implementing Zend_Validate_Interface, but apparently Zend Form only sends in the value of the current field to the validator. How do I make the validator receive everything?

推荐答案

Zend_Form上调用isValid时,它将把传递给该方法的所有数据传递给该

When you call isValid on a Zend_Form it will pass the all the data you passed to the method

$form->isValid(array('a' => 1, 'b' => 2));

您的自定义验证器将收到整个原始值数组.

Your custom validator will receive that whole array of raw values.

示例验证器

class My_Validator_TwoVals implements Zend_Validate_Interface
{
    public function getMessages()
    {
        return array();
    }
    public function isValid($value)
    {
        print_r(func_get_args());
    }
}

示例表格

$f = new Zend_Form;
$a = $f->createElement('Text', 'a');
$b = $f->createElement('Text', 'b');
$b->addPrefixPath('My_Validator', '.', 'validate');
$b->addValidator('TwoVals');
$f->addElements(array($a, $b));

$f->isValid(array('a' => 1, 'b' => 2));

输出

Array
(
    [0] => 2
    [1] => Array
        (
            [a] => 1
            [b] => 2
        )
)

如您所见,还有另一个参数传递给isValid,即$ context.并包含剩余的值.

As you can see there was also a second argument passed to isValid, which is $context. And that contains the remaining values.

替代是将与之匹配的第二个元素作为选项传递给验证器,例如

An alternative would be to pass the second element to match against as an option to the Validator, e.g.

class My_Validator_TwoVals implements Zend_Validate_Interface
{
    protected $a;
    public function getMessages()
    {
        return array();
    }
    public function isValid($value)
    {
        var_dump($this->a->getValue());
    }
    public function __construct(Zend_Form_Element $a)
    {
        $this->a = $a;
    }
}

设置

$f = new Zend_Form;
$a = $f->createElement('Text', 'a');
$b = $f->createElement('Text', 'b');
$b->addPrefixPath('My_Validator', '.', 'validate');
$b->addValidator('TwoVals', false, array($a));
$f->addElements(array($a, $b));

$f->isValid(array('a' => 1, 'b' => 2));

然后将打印int(1).如您所见,我们通过form元素的API提取了该值,因此可以应用您为验证器和过滤器配置的所有内容,例如这不是原始价值.您还可以将其设置为其他值,等等.

Will then print int(1). As you can see, we fetched that value through the form element's API so anything you configured for validators and filters would be applied, e.g. it's not the raw value. And you can also set it to another value, etc.

也请参阅Zend_Validate_Identical,以了解ZF如何实施其他表单元素的检查:

Also have a look at Zend_Validate_Identical to learn how ZF implements checking of other form elements:

这篇关于如何根据另一个字段的值验证Zend_Form的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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