Symfony 1.4:检查表单类中是否有错误



Symfony 1.4中是否有一种简单的方法来知道提交的表单在表单类中是否有任何错误?我熟悉模板的$form['some_field']->hasErrors(),但在这种情况下,只有在表单与标准验证器没有任何错误的情况下,我才想运行后验证器。我基本上是在追求这样的东西:

public function configure() {
  // widgets
  // standard validators
  if (!this->hasErrors()) {
    // run post-validator
  }
}

API 文档像往常一样神秘。提前谢谢。

由于

验证在bind调用中是执行的,因此除了在bind函数中之外,我没有看到其他地方可以发布错误验证。因此,在您的表单类中:

public function bind(array $taintedValues = null, array $taintedFiles = null)
{
  parent::bind($taintedValues, $taintedFiles);
  if ($this->hasErrors())
  {
    // do post validate
    // you can access values from your form using $taintedValues
  }
}

但是您必须手动调用验证器,而不仅仅是定义一个新的验证器(因为绑定过程已经完成)。

最新更新