duanmengsuo9302 2011-09-28 14:58
浏览 18
已采纳

如何根据另一个字段的值验证Zend_Form的字段?

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%.

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?

  • 写回答

1条回答 默认 最新

  • douzen1896 2011-09-28 15:53
    关注

    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.

    Example Validator

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

    Example Form

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

    Output

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

    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;
        }
    }
    

    Setup

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

    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.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制