1

I'm trying to define a specific new block for form field errors rendering, keeping form_errors unchanged for common errors rendering.

# Twig Configuration
twig:
  debug:            %kernel.debug%
  strict_variables: %kernel.debug%
    form:
      resources:
        - 'ApplicationMyBundle:Main:form/customFormTheme.html.twig'

In customFormTheme.html.twig I overwrite a few blocks copied from form_div_layout.html.twig plus I added the folloowing new one.

{% block field_errors %}{% spaceless %}
        {% if errors|length > 0 %}
        <ul class="errors">
            {% for error in errors %}
                {% if error.messageTemplate|length %}
                <li class="error">{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li>
                {% endif %}
            {% endfor %}
        </ul>
        {% endif %}
{% endspaceless %}{% endblock %}

Then I expect to be able to use this block in my views like this :

 <div>
     {{ form_label(form.message, 'message.label'|trans({},'contact')|raw ) }}
     {{ form_widget(form.message, {attr: {maxlength:1000, size:1000, rows:8}}) }}
     {{ field_errors(form.message) }}
 </div>

but I receive the following error :

The function "field_errors" does not exist. Did you mean "form_errors"

I also tried by naming my block text_errors or textarea_errors mentioned here but I haven't been luckier.

Any idea ?

1 Answer 1

2

Actually it works by defining the block text_errors or textarea_errors only and still use {{ form_errors(field.name) }} in your template. If a block named after the type of your field exists (according to form field types) it will be used instead of form_errors.

!! But you can't use directly {{ text_errors(field.name) }} in your twig template !!

The same way you can have a custom row for a specific type like this

{% block textarea_row %}{% spaceless %}
    <div class="textarea l-field {{ (form_errors(form)?'error':'') }}">
        {{ form_label(form) }}
        {{ form_widget(form) }}
        {{ form_errors(form) }}
    </div>
{% endspaceless %}{% endblock textarea_row %}

and use it in your template as follow :

{# message has textarea field type #}

{{ form_row(form.message, {
    label: 'message.label'|trans({},'contact')|raw ,
    attr: {maxlength:1000, size:1000, rows:8}})
}}

You can also pass many custom parameters by using the object attr{}

{% block form_row %}
{% spaceless %}
    <div class="form-field {{ (form_errors(form)?'error':'') }}">
        {{ form_label(form) }}
        {{ form_widget(form) }}
        {{ dump(attr) }}
        {% if attr.help is defined and not attr.help == '' %}<p class="form-help">{{ attr.help }}</p>{% endif %}
        {{ form_errors(form) }}
    </div>
{% endspaceless %}
{% endblock form_row %}

and use it like this

{{ form_row(form.message, {
       label: 'message.label'|trans({},'contact')|raw ,
       attr: {
           maxlength:1000, size:1000, rows:8,
           help: 'password.help'|trans({})|raw
       }
   })
}}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm still missing a way to had custom parameters to test in my custom block and have further customisations possible like with help:"A custom legend for the field".
I figured out how to do so by using attr object. I edited my answer. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.