Macbernie
Macbernie

Reputation: 1323

Selected value in Twig

I try to selected values in my form with twig:

    {{ form_widget(form.item1, {value: "1" }) }}
    {{ form_widget(form.item2, {value: "4" }) }}
    {{ form_widget(form.item3, {value: "11" }) }}

This is working, but how can I pass variable argument ?

    {{ form_widget(form.item1, {value: myvariable1 }) }}
    {{ form_widget(form.item2, {value: myvariable2 }) }}
    {{ form_widget(form.item3, {value: myvariable3 }) }}

Doesn't work...

Thanks for help

Upvotes: 1

Views: 3312

Answers (1)

Jovan Perovic
Jovan Perovic

Reputation: 20193

I think you can't do it from Twig, neither you should.

Try assigning the data option of form field:

$builder->add('field', 'choice', array(
    ....
    'data' => 'some_existing value' <-- THIS
    ....
));

Update:

It seemed that the issue was with type mismatch: 1 != "1".

The resolution was to force conversion of int to string:

{{ form_widget(form.item1, {value: myvariable ~ ""}) }}

Upvotes: 4

Related Questions