doutan5337 2013-11-19 19:59
浏览 46

如何在symfony 2.3中自定义全局表单错误?

The Symfony 2.3 docs say that it is possible to customize global form errors (errors that appear at top of form that are not tied to a specific field).

http://symfony.com/doc/current/cookbook/form/form_customization.html#customizing-error-output

"You can also customize the error output for just one specific field type. For example, certain errors that are more global to your form (i.e. not specific to just one field) are rendered separately, usually at the top of your form..."

They go on to say that:

"To customize only the markup used for these errors, follow the same directions as above, but now call the block form_errors"

I am confused as to what they are talking about. It seems that their prior instructions already call the block "form_errors" so I am not sure what is different.

How do I customize just the global form errors (the individual form field errors should remain the same)?

  • 写回答

1条回答 默认 最新

  • doutou1922 2014-10-06 23:37
    关注

    The wording on that page is confusing. I had to read it a few times too, and it feels like there should be more information. The example provided shows how to check if the error is a "compound" error (for the whole form) or just an error for the individual field.

    This is how my block looks:

    {% block form_errors %}
        {% if errors|length > 0 %}
            <ul class="alert alert-warning {% if compound %}formError{% else %}formInputError{% endif %}">
                {% for error in errors %}
                    <li>{{ error.message }}</li>
                {% endfor %}
            </ul>
        {% endif %}
    {% endblock form_errors %}
    

    If it's a compound form, it renders the class as formError, and if it's just for an input it renders formInputError. That let's me style them slightly different, depending on if they appear at the top of the page or above an input. You could also make them completely separate HTML if you needed.

    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错