无法让自定义错误渲染在 symfony 1.4 中工作

发布于 2024-08-29 00:09:58 字数 847 浏览 3 评论 0原文

我尝试根据 这个例子

这是我的代码:

if ($this['message']->hasError()) {
  $error_msg = '<ul>';
  foreach ($this['message']->getError() as $error) $error_msg .= "<li>$error</li>";
  $error_msg .= '</ul>';
}

return $error_msg;

但是当 $this['message'] 出现错误时,此代码返回 '

    ' 所以看起来 foreach ($this['message']->getError() as $error) 不会导致迭代

    $this['message']->getError() 返回 sfValidatorError 对象 - 也许 symfony 1.4 中发生了一些变化,它不再是可迭代的...

    起初我认为该示例中的所有魔法都发生了,因为对象被放置在 $error 中迭代实现了 __toString() 但似乎根本没有发生迭代......

    I'm tring to customize error rendering in my form according to this example.

    Here is my code:

    if ($this['message']->hasError()) {
      $error_msg = '<ul>';
      foreach ($this['message']->getError() as $error) $error_msg .= "<li>$error</li>";
      $error_msg .= '</ul>';
    }
    
    return $error_msg;
    

    but when $this['message'] has error this code returns '<ul></ul>' so it seems foreach ($this['message']->getError() as $error) causes no iterations

    $this['message']->getError() returns sfValidatorError object - maybe something changed in symfony 1.4 and it isn't iterable anymore...

    At first I thought that all magic in that example happened because of object being placed in $error by iteration implements __toString() but it seems no iterations happens at all...

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

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

    发布评论

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

    评论(1

    把回忆走一遍 2024-09-05 00:09:58

    您应该有变量 $form 来保存您正在显示的表单,并且代码应该是 $form['message']->hasError() 和 $form['message']->getError() ,其中“消息”是表单中的一个小部件。不要使用 $this,因为它不包含此上下文中的形式。

    you should have variable $form which holds the Form that you're displaying, and the code should be $form['message']->hasError() and $form['message']->getError() , where 'message' is a widget in your form. Do not use $this, as it doesn't hold the form in this context.

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