Maybe I’m overlooking something, and hopefully this is done very easy.
I have a form and what I want in the end is the following result:
Fields which:
- are mandatory/required
- have an error currently
- have help
should get an extra a-Tag after the label and an extra div, filled with the help and/or the error, if applicable.
What I got to work is, that required fields get the a-Tag by using this:
{% use 'form_div_layout.html.twig' with field_label as base_field_label %}
{% block field_label %}
{{ block('base_field_label') }}
{% if required %}
<a href=""><span> </span></a>
{% endif %}
{% endblock %}
So, what I tried already were different versions of this:
{% use 'form_div_layout.html.twig' with field_label as base_field_label %}
{% block field_label %}
{{ block('base_field_label') }}
{% if required or help is defined %}
<a href=""><span> </span></a>
{% endif %}
{% endblock %}
{% block field_row %}
{% spaceless %}
<div class="row">
{% if required or help is defined %}
<div>
{{ form_errors(form) }}
{{ help }}
</div>
{% endif %}
{{ form_label(form) }}
{{ form_widget(form, { 'attr': {'class': 'grid_4'} }) }}
</div>
{% endspaceless %}
{% endblock field_row %}
And I can’t get this to work.
So my questions are:
-
Where do I get the help text from, which can also contain HTML? I tried this within the
form builderwithout success – but at least with an exception:$builder ->add('subject', 'text', array( 'label' => 'Subject', 'help' => 'Can be formatted content with <strong>HTML-Elements</strong>', )); -
How can I tell that the current field has an error (to add a class to the row) and if so also display it?
{{ form_errors(form) }}did not output anything, no matter where I place it within `field_row˚.
There is no help text, you have to create Form Extension for field and add it to default options.
Example in SF 2.1 Beta 1:
And register it as a service:
For the errors question:
Do you have any errors to print? Check that in controller if validation fails: