Symfony:如果正常验证失败,则停止验证

时间:2011-06-26 15:16:33

标签: php validation symfony1

如果在“正常”验证中发生错误,是否有办法阻止表单进入后验证?

2 个答案:

答案 0 :(得分:1)

您需要使用回调来验证表单...例如:

public function configure()
{
  $this->widgetSchema['email'] = new sfWidgetFormInput();
  $this->validatorSchema['email'] = new sfValidatorEmail(array('required' => true));

  $this->validatorSchema->setPostValidator(
     new sfValidatorCallback(array('callback' => array($this, 'postValidate')))
  );
}

public function postValidate($validator, $values)       
{
  // only run the post validator if the email has passed validation
  // if it hasn't passed validation, it will be blank and we can skip this
  if ($values['email'])
  {
    $v = new myPostValidator();
    $v->clean($values);
  }

  return $values;
}

答案 1 :(得分:0)

这取决于你对“正常”和后验证的意思:

在动作类中,您可以使用以下条件:

if ($form->isValid()) { // code here before and after the save(); }

或者你在行动中使用了postExecute吗?