This question already has an answer here:
I'm here with a quick question, because I can't figure out if I'm doing something wrong or Symfony2 Forms
component does not work consistently. The scenario is as follows:
- I have a form with custom form type [
ChoiceType
"child" type] with some predefined values - I need this custom form type to have default value selected when no value has been selected before [so the form has not yet been submitted]
- Then again, I may also have this form prepopulated based on user's settings [preferences]
What I would expect to happen:
- No user preferences - form field has default value [0]
- User preferences available - form field has value passed in
$data
parameter [0-7] - Form is being submitted - the value of form field is the one that has been submitted [0-7]
What actually happens:
- If form field is set to be select
['expanded' => false]
it works as expected for both display (first value [0] is selected - probably because it's the first one) and submission (0 is being submitted) - If form field is radio button list
['expanded' => true]
none of the values are selected to begin with and submitting the form does not submit any value either.
Can someone explain me how to make radio button selected? I can use empty_data
to set the value if none is submitted, but what about initial form display? It would be perfect to make the selection (for both select boxes and radio buttons) based on empty_value
.
I also can't use data
option, because it will overwrite any data passed to form (see point 2 above) as well as data submitted.
@EDIT
I would like to avoid two things:
- using JavaScript is definitely not an option
- overriding the template is something I'd like to avoid as well, mainly because I'm currently using bootstrap theme as a foundation which is going to be replaced by proper frontend / templates later
</div>