如何在嵌入式表单的后验证器(sfForm)中抛出错误

发布于 2024-09-14 22:03:12 字数 781 浏览 3 评论 0原文

使用 Symfony 1.4 的 Forms,如何在嵌入表单的后验证器中抛出 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() ,但它不应该是。

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

With Symfony 1.4's Forms, how can I throw a sfValidatorError in the post validator of an embedded form?

My parent form calls the following:

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

And my embedded form:

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.');

}

The post validator runs validateLink which throws sfValidatorError but it does not show up as a global error and the form isValid(), but it shouldn't be.

Why is the error ignored? How can I make it not ignored?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随风而去 2024-09-21 22:03:12

恕我直言,最好抛出一个 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));

IMHO it's better to throw a sfValidatorSchemaError, like this :

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

And if you want to throw an error inside an embedded form, just embed the 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));
少钕鈤記 2024-09-21 22:03:12

在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');
     }
   }
 }

我希望它对你有帮助。

in sf1.1 I do it like this:

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

I hope it helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文