Commit
RadioListMapper::mapDataToForm()
This fixes "false" choice pre selection when `ChoiceType` is `expanded` and not `multiple`
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,14 @@ public function buildForm(FormBuilderInterface $builder, array $options) | |
$event->setData(null); | ||
} | ||
}); | ||
// For radio lists, pre selection of the choice needs to pre set data | ||
// with the string value so it can be matched in | ||
// {@link \Symfony\Component\Form\Extension\Core\DataMapper\RadioListMapper::mapDataToForms()} | ||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { | ||
$choiceList = $event->getForm()->getConfig()->getOption('choice_list'); | ||
$value = current($choiceList->getValuesForChoices(array($event->getData()))); | ||
$event->setData((string) $value); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
HeahDude
Author
Contributor
|
||
}); | ||
} | ||
} elseif ($options['multiple']) { | ||
// <select> tag with "multiple" option | ||
|
@HeahDude Why does this have to be cast to a string? This causes data transformers to never have it's
$value
set toNULL
(since they happen after thePRE_SET_DATA
event). A lot of libraries check if the$value
isNULL
.@fabpot In the symfony docs the implementation of a data transformer is that is checks the value to be null, here a reference of the official docs:
A lot of data transformers of other libraries have that strict if statement and it breaks with the current changes. Is a good solution to do a less strict check for empty values, such as
if (empty($value))
? What is your opinion on this?