Symfony 1.4 - 强制symfony在到达后验证器之前停止执行

时间:2013-10-10 23:42:31

标签: php symfony-1.4

我在symfony 1.4中创建了一个丢失的密码表单,并且一直在遇到调用post验证器的问题,即使小部件约束失败也是如此。

段:

$this->validatorSchema['email_address'] = new sfValidatorEmail(
    array(
        'max_length'    => 254,  // http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690
        'min_length'    => 4,
        'required'      => true,
        'trim'          => true
    ),
    array(
        'invalid'       => 'Email address entered is malformed',
        'max_length'    => 'Email Address cannot contain more than %max_length% characters',
        'min_length'    => 'Email Address must be at least %min_length% characters',
        'required'      => 'Please enter an email address'
    )
);

$this->mergePostValidator(new sfValidatorCallback(array('callback' => array($this, 'emailValidate'))));
$this->mergePostValidator(new sfValidatorCallback(array('callback' => array($this, 'throttleCheck'))));

问题是'emailValidate'回调假定已输入格式正确的电子邮件地址,如a@b.com,但如果我输入垃圾,如'fdadfaa',表单仍会继续尝试执行帖子验证器,什么时候不应该。如果数据不好,没有理由发出查询。

found一个'halt_on_error'标志,可以传递给模式验证器构造函数,但对我不起作用。还有人穿过这座桥吗?

1 个答案:

答案 0 :(得分:0)

Symfony是一个疯狂的旅程...能够使用它来实现它:

$this->validatorSchema['email_address'] = new sfValidatorAnd(
    array(
        new sfValidatorEmail(
            array(
                'max_length'    => 254,  // http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690
                'min_length'    => 4,
                'required'      => true,
                'trim'          => true
            ),
            array(
                'invalid'       => 'Email address entered is malformed',
                'max_length'    => 'Email Address cannot contain more than %max_length% characters',
                'min_length'    => 'Email Address must be at least %min_length% characters'                        
            )
        ),
        new sfValidatorCallback(array('callback' => array($this, 'emailValidate'))),
        new sfValidatorCallback(array('callback' => array($this, 'throttleCheck')))
    ),
    array('halt_on_error' => true),
    array('required' => 'Please enter an email address')
);

这里唯一的问题是,postValidators直接绑定到电子邮件字段,因此您必须更改$values['email']$values的所有引用。但它似乎在点击后验证器之前停止执行流程。