Tesmen
Tesmen

Reputation: 589

Custom radio button id in Twig

Code of rendered Twigs radio button set form usually looks like...

This:

 {{ form_widget(formTitle.radioSet)  }}

To this...

<input type="radio" id="formTitle_radioSet_0" name="formTitle[radioSet]" value="fooValue">
<input type="radio" id="formTitle_radioSet_1" name="formTitle[radioSet]" value="booValue">

... and a few more rows like this

I want to make it work with JS easier. Is it possible to render customized radio buttons with IDs reffered to the value of radio and my own preferences like this??

<input type="radio" id="radioSet_fooValue" name="formTitle[radioSet]"  value="fooValue">
<input type="radio" id="radioSet_booValue" name="formTitle[radioSet]"  value="fooValue">

Upvotes: 1

Views: 6111

Answers (2)

Tesmen
Tesmen

Reputation: 589

After about half an hour of exploring article recommended by Carlos Granados got a solution. The target is to override twigs template of renderind radio inputs.

In a few words, code below, pasted at the beginning of your web page template will override ALL radio's render the way it described in question.

{% form_theme form _self %}
{%- block radio_widget -%}
<input type="radio" name="{{ full_name }}" id="radio_{{ value }}" value="{{ value }}"{% if checked %} checked="checked"{% endif %} />
{%- endblock radio_widget -%}

Upvotes: 2

Carlos Granados
Carlos Granados

Reputation: 11351

Use custom form rendering

You will just need to customize the widget_radio block using one of the techniques described in that cookbook entry

Upvotes: 1

Related Questions