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:
ChoiceType
"child" type] with some predefined valuesWhat I would expect to happen:
$data
parameter [0-7]What actually happens:
['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)['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:
</div>
I'm not sure to understand your question. To manage the form default value, you only need to set the property of the object mapped.
For example:
$formObject->child->attributeDefault = "defaultValue";
if($hasPreference) {
$formObject->child->attribute1 = "preferenceDefautlValue"
}
$form = $this->createForm(new YourFormType(), $formObject);
$form->handleRequest($request) //overide default value by submitted one