0
votes

I'm using angularjs and for that purpose I need to assign ng-model to my radio field.

However, with form_widget all the attributes are assigned to the container (div) instead of the real input fields. I got as far as overriding the radio_widget block but not quite sure how to pass attributes into this radio_widge t from the form widget?

2

2 Answers

1
votes

Thank you mr1031011,

my fault. I have double checked it and in conclusion you need to overwrite form_div_layout.html.twig - you can find more about extending form layout in here http://symfony.com/doc/current/cookbook/form/form_customization.html .

I have added attr to one of the blocks - it seems to be working now.

{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {{ form_widget(child, { 'attr': {'class': 'subclass', 'rel': 'test'} }) }}
        {{ form_label(child) }}
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}

The only one difference between the code from core and mine is , { 'attr': {'class': 'subclass', 'rel': 'test'} } is added. Of course you can generalize this part by adding some options like subattr or something like that, but in this example you can find an idea.

I hope that will help

regards.

0
votes

on the official website you can find

{{ form_widget(form.task, { 'attr': {'class': 'task_field'} }) }}

after all it may look like:

<div>
    {{ form_label(form.name) }}
    {{ form_errors(form.name) }}
    {{ form_widget(form.name, { 'attr': {'class': 'task_field'} }) }}
</div>

It's the simplest way to provide this. On the other hand you can create own template for radio fields and add your required attributes.

regards.