Symfony2 form_errors

后端 未结 4 776
情书的邮戳
情书的邮戳 2021-02-06 14:51

I think its a simple question. Its about outputting errors. This is my twig file:

    
{{ form_label(form.d
4条回答
  •  天涯浪人
    2021-02-06 15:29

    You can customize how form errors render by providing your own form theme with a field_errors block.

    You can do this for just the current template:

    {# tell the form to look for theme blocks in the current template #}
    {% form_theme form _self %}
    
    {% block field_errors %}
    {% for error in errors %}
    {{ error.messageTemplate|trans(error.messageParameters, 'validators') }}
    {% endfor %} {% endblock %} {# the rest of your template... #}

    Or by defining a global form theme in config.yml:

    twig:
        form: { resource: "::form_theme.html.twig" }
    

    In this case you would want to move the field_errors block above to app/Resources/views/form_theme.html.twig and the form_theme tag is no longer necessary.

提交回复
热议问题