【问题标题】:Display all errors with form_errors(form) plus for each field in symfony2使用 form_errors(form) plus 显示 symfony2 中每个字段的所有错误
【发布时间】:2012-10-22 07:40:12
【问题描述】:

我需要在表单上方显示所有错误,并为每个字段显示单独的错误。我该怎么做?

【问题讨论】:

    标签: forms symfony error-handling


    【解决方案1】:

    您需要更具体,但希望以下内容可以帮助您。

    假设您有一个名为 form 的变量。

    {{ form_errors(form) }} 显示并非特定于一个字段的全局错误

    {{ form_errors(form.email) }} 显示特定于字段的错误

    {{ form_row(form.email) }} 为字段显示 form_widget form_label 和 form_errors

    http://symfony.com/doc/2.0/cookbook/form/form_customization.html

    编辑:

    因此,如果您希望将全局错误和字段错误显示在同一位置,您可以这样做:

    {{ form_errors(form) }}
    {{ form_errors(form.field1) }}
    {{ form_errors(form.field2) }}
    ...
    

    【讨论】:

      【解决方案2】:
      {% spaceless %}
          {% if not form.vars.valid %}
                  <div class="alert alert-error">
                      {{ form_errors(form) }}
      
              {% for children in form.children %}
                  {% if not children.vars.valid %}
                      {{ form_errors(children) }}
      
                      {# or with field label
                      <ul>
                          {% for error in children.vars.errors %}
                              <li><b>{{ children.vars.label }}</b>: {{ error.message }}</li>
                          {% endfor %}
                      </ul>
                      #}
                  {% endif %}
              {% endfor %}
                  </div>
          {% endif %}
      {% endspaceless %}
      

      在 sf 2.3 中为我工作

      【讨论】:

        【解决方案3】:

        在 Symfony 3.2 中,要获取模板中的所有表单错误,您可以使用 form.vars.errors.form.getErrors(true) 使用有点 hacky 但简单且有效的解决方案:

        <ul>
            {% for error in formView.vars.errors.form.getErrors(true) %}
            <li>{{ error.message }}</li>
            {% endfor %}
        </ul>
        

        诀窍在于:

        1. 原始表单对象通过错误迭代器 (formView.vars.errors.form),
        2. form.getErrors(true) 为您提供了一个遍历所有表单错误的递归迭代器。

        【讨论】:

          【解决方案4】:

          我在我的包中覆盖了 form_div_layout.html.twig:

          {% block form_errors %}
              {% spaceless %}
                  {% set a = false %}
                  {% for child in form.children  %}
                      {% if child.get("errors") %}
                          {% set a = 'true' %}
                      {% endif %}
                  {% endfor %}
                  {% if a == true %}
                      <div class="alert">
                          {% for children in form.children %}
                              {{  form_errors(children) }}
                          {% endfor %}
                      </div>
                  {% endif %}
                  {% if errors|length > 0 %}
                      <ul>
                          {% for error in errors %}
                              {{
                              error.messagePluralization is null
                              ? error.messageTemplate|trans(error.messageParameters, 'validators')
                              : error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
                              }}
                          {% endfor %}
                      </ul>
                  {% endif %}
              {% endspaceless %}
          {% endblock form_errors %}
          

          现在如果写form_errors(form),它会在表单中显示所有错误,并且每个字段上的错误也会指示。

          【讨论】:

          • child.get("errors") 不适用于 Symfony 2.5 有人有解决方案吗?
          • 在新版本上使用 vars.errors 或直接转储表单字段以查看属性。
          【解决方案5】:

          您的表单和字段都有单独的错误字段开始。您能否更具体地说明您要做什么以及您的问题出在哪里?

          【讨论】:

          • 我需要每个字段的相同错误将显示在顶部表单上(所有错误都在一个地方)并调用 form_errors(form),这可能吗?
          • Daniel 已经向您发布了表单文档。它应该可以帮助您找到满足您需求的东西。
          • 它不能解决我的问题。我需要与我的表单在所有表单中的每个字段相同的错误。 {{ form_errors(form) }} 仅显示不特定于一个字段的全局错误。
          • 我的回答确实解决了你的问题。阅读文档,它会告诉您如何操作。即使是我自己的答案也应该足够了。我更新了我的答案,希望你现在明白了。如果在理解之前不阅读文档。
          【解决方案6】:

          我修改了@korvinko 的脚本,这适用于 Symfony 2.6.11 `

          {% block form_errors %}
              {% spaceless %}
                  <ul>
                      {% for children in form.children %}
                          {% if not children.vars.valid %}
                             {% for error in children.vars.errors %}
                                  <li>{{ children.vars.label ~ ' ' ~
                                  error.messagePluralization is null
                                  ? error.messageTemplate|trans(error.messageParameters, 'validators')
                                  : error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
                                  }}</li>
                              {% endfor %}
                          {% endif %}
                      {% endfor %}
                  </ul>
          
                  {% if errors|length > 0 %}
                      <ul>
                          {% for error in errors %}
                              <li>{{
                              error.messagePluralization is null
                              ? error.messageTemplate|trans(error.messageParameters, 'validators')
                              : error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
                              }}</li>
                          {% endfor %}
                      </ul>
                  {% endif %}
              {% endspaceless %}
          {% endblock form_errors %}
          

          `

          【讨论】:

          • 这不再适用于 Symfony 2.7.1。 'children.vars.valid'(第 5 行)不存在
          • 你可以用{% if children.vars.errors|length &gt; 0 %}替换{% if not children.vars.valid %}
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-22
          • 2016-01-27
          相关资源
          最近更新 更多