【问题标题】:Zend_Validate's isValid() method, and the $_POST arrayZend_Validate 的 isValid() 方法和 $_POST 数组
【发布时间】:2011-07-06 01:48:54
【问题描述】:

在一本关于 Zend 框架的书中,我遇到了一个用于唯一电子邮件地址的自定义验证器。验证器扩展了 Zend_Validate_Abstract,因此实现了 isValid() 接口。

在接口中方法签名是isValid($value)。在具体类中,它是 isValid($value, $context = null)。

作者解释说 $context 变量包含 $_POST 数组,他在方法实现中依赖于数组中的值。但是,当我尝试重现代码时,我的 $context 数组为空。此外,我无法在 Zend_Validation 参考指南中找到对 $context 的任何引用,也无法找到传递 $_POST 值。

还有其他人遇到过这个吗?

顺便说一句,这本书是 Keith Pope 的 Zend Framework 1.8 Web Application Development。

谢谢!

【问题讨论】:

    标签: zend-framework zend-validate


    【解决方案1】:

    $context 数组在与Zend_Form_Element::isValid 方法一起使用时被传递

    这是来自Zend_Form_Element::isValid 的sn-p,它通过$_POST 作为$context,通常从Zend_Form::isValid 接收

                foreach ($value as $val) {
                    if (!$validator->isValid($val, $context)) {
                        $result = false;
                        if ($this->_hasErrorMessages()) {
                            $messages = $this->_getErrorMessages();
                            $errors   = $messages;
                        } else {
                            $messages = array_merge($messages, $validator->getMessages());
                            $errors   = array_merge($errors,   $validator->getErrors());
                        }
                    }
                }
    

    【讨论】:

      【解决方案2】:

      看起来 Zend_Validate 的文档不完整。

      Zend_Form_Element (ctl-f "Validation Context") 的手册中描述了此行为

      【讨论】:

        猜你喜欢
        • 2012-01-20
        • 2014-08-03
        • 2016-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 2010-11-26
        • 1970-01-01
        相关资源
        最近更新 更多