-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Fix ChoiceType to ensure submitted data is not nested unnecessarily #21267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cd7d2a4
fb33ad2
98195dd
9d406db
61c80c2
eb4d85a
d6d2db5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,22 @@ public function buildForm(FormBuilderInterface $builder, array $options) | |
// transformation is merged back into the original collection | ||
$builder->addEventSubscriber(new MergeCollectionListener(true, true)); | ||
} | ||
|
||
// To avoid issues when the submitted choices are arrays (i.e. array to string conversions), | ||
// we have to ensure that all elements of the submitted choice data are strings or null. | ||
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { | ||
$data = $event->getData(); | ||
|
||
if (!is_array($data)) { | ||
return; | ||
} | ||
|
||
foreach ($data as $v) { | ||
if (null !== $v && !is_string($v)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The is_string() check may be a little bit to restrictive in a loosely typed language like PHP. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #21957 which is part of the 2.8.19 release There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks :) |
||
throw new TransformationFailedException('All choices submitted must be NULL or strings.'); | ||
} | ||
} | ||
}, 256); | ||
} | ||
|
||
/** | ||
|
@@ -505,8 +521,8 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op | |
* "choice_label" closure by default. | ||
* | ||
* @param array|\Traversable $choices The choice labels indexed by choices | ||
* @param object $choiceLabels The object that receives the choice labels | ||
* indexed by generated keys. | ||
* @param object $choiceLabels the object that receives the choice labels | ||
* indexed by generated keys | ||
* @param int $nextKey The next generated key | ||
* | ||
* @return array The choices in a normalized array with labels replaced by generated keys | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding a new event listener we could do this in the already existing one before flipping the data, can't we?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The listener you mentioned is only added when choice is multiple. Even though we would do so we still need to add a new listener for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I missed that.