Skip to content

Commit

Permalink
Fix loading new item list after adding new created element on sortabl…
Browse files Browse the repository at this point in the history
…e collections sonata-project#8201

Apply fix as proposed in
sonata-project#8201 (comment)
  • Loading branch information
BA-JBI authored Aug 15, 2024
1 parent c7a0016 commit fc76bd0
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Form/Extension/ChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@
*/
final class ChoiceTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if ($options['multiple'] && (true === ($options['sortable'] ?? false))) {
$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event) use ($options) {
/** @var PreSubmitEvent $event */
$form = $event->getForm();
$data = $event->getData();

if (!is_array($data) || count($data) !== 1) {
return;
}

if (str_contains($data[0], ',')) {
$event->setData(explode(',', $data[0]));
}
},1);
}
}

public function configureOptions(OptionsResolver $resolver): void
{
$optionalOptions = ['sortable'];
Expand Down

0 comments on commit fc76bd0

Please sign in to comment.