1

使用 Symfony 1.4 的表单,如何在嵌入式表单的验证器中抛出 sfValidatorError?

我的父表单调用以下内容:

public function configure(){
    $this->embedForm('page', $pageLinkForm);
}

还有我的嵌入式表格:

public function configure(){
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array(
        'callback' => array($this, 'validateLink')
    )));
}

public function validateLink($validator, $values) {
    if (!empty($values['link']) && !empty($values['outside_link']))
        throw new sfValidatorError($validator, 'Only specify either an internal link or an external link, but not both.');

}

后验证器运行 validateLink,它会抛出 sfValidatorError,但它不会显示为全局错误和表单 isValid(),但它不应该是。

为什么错误被忽略?我怎样才能让它不被忽视?

4

2 回答 2

2

恕我直言,最好抛出 sfValidatorSchemaError,如下所示:

$error =  new sfValidatorError($validator, 'invalid', array('value' => $field_name));
throw new sfValidatorErrorSchema($validator, array($field_name => $error));

如果你想在嵌入的表单中抛出错误,只需嵌入 sfValidatorSchemaError :

//define container
$errorSchema =  new sfValidatorErrorSchema($validator);

//embedded field error
$error =  new sfValidatorError($validator, 'invalid', array('value' => $field_name));
$errorSchema->addError($error, $field_name);

//associate $errorSchema to your embedded field
throw new sfValidatorErrorSchema($validator, array('page' => $errorSchema));
于 2011-08-23T15:20:46.180 回答
0

在 sf1.1 我这样做:

 public function bind(array $taintedValues = null, array $taintedFiles = null)
 {
   sfLoader::loadHelpers(array('I18N'));
   parent::bind($taintedValues, $taintedFiles);
   if($taintedValues["password"])
   {
     if(!$taintedValues["pwd_verify"])
     {
       $this->getErrorSchema()->addError(new sfValidatorError(new sfValidatorSchema(), __('Please reenter the new password.')), 'password');
     }
   }
 }

我希望它对你有帮助。

于 2010-08-23T19:21:49.473 回答