From a46c85d66cb5b13ddda1cb6b4698ad7e5f2b507d Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 16 Oct 2022 13:21:03 +0200 Subject: [PATCH] Fix nested forms problems with select2 FormTypes in issue 641 and 649 Select2UserLocationType.php is called in another context: UserController call UserCurrentLocationType which instanciate new Select2UserLocationType.php FormType --- .../Controller/UserController.php | 4 +-- .../Form/Type/Select2LocationTypeType.php | 35 +++++++------------ .../Form/Type/Select2UserLocationType.php | 26 +++++--------- .../Form/UserCurrentLocationType.php | 26 ++++++++++++++ .../Form/Type/Select2SocialActionType.php | 35 +++++++------------ .../Form/Type/Select2SocialIssueType.php | 35 +++++++------------ 6 files changed, 75 insertions(+), 86 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php diff --git a/src/Bundle/ChillMainBundle/Controller/UserController.php b/src/Bundle/ChillMainBundle/Controller/UserController.php index 03d8d2692..9d3941411 100644 --- a/src/Bundle/ChillMainBundle/Controller/UserController.php +++ b/src/Bundle/ChillMainBundle/Controller/UserController.php @@ -15,7 +15,7 @@ use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\Entity\GroupCenter; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Form\Type\ComposedGroupCenterType; -use Chill\MainBundle\Form\Type\Select2UserLocationType; +use Chill\MainBundle\Form\UserCurrentLocationType; use Chill\MainBundle\Form\UserPasswordType; use Chill\MainBundle\Form\UserType; use Chill\MainBundle\Pagination\PaginatorInterface; @@ -234,7 +234,7 @@ class UserController extends CRUDController public function editCurrentLocationAction(Request $request) { $user = $this->getUser(); - $form = $this->createForm(Select2UserLocationType::class, $user) + $form = $this->createForm(UserCurrentLocationType::class, $user) ->add('submit', SubmitType::class, ['label' => 'Save']) ->handleRequest($request); diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php index 783ec4e5c..33121f8bb 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php @@ -15,7 +15,6 @@ use Chill\MainBundle\Entity\LocationType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class Select2LocationTypeType extends AbstractType @@ -27,33 +26,25 @@ class Select2LocationTypeType extends AbstractType $this->translatableStringHelper = $translatableStringHelper; } - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder->add('locationtype', EntityType::class, [ - 'class' => LocationType::class, - 'choice_label' => function (LocationType $type) { - return $this->translatableStringHelper->localize($type->getTitle()); - }, - 'placeholder' => 'Pick a location type', - 'required' => false, - 'label' => $options['label'], - 'label_attr' => $options['label_attr'], - 'multiple' => $options['multiple'], - 'attr' => ['class' => 'select2'], - ]); - } - public function configureOptions(OptionsResolver $resolver) { $resolver - ->setDefault('label', 'Location type') - ->setDefault('label_attr', []) - ->setDefault('multiple', false) + ->setDefaults([ + 'class' => LocationType::class, + 'choice_label' => function (LocationType $type) { + return $this->translatableStringHelper->localize($type->getTitle()); + }, + 'placeholder' => 'Pick a location type', + 'required' => false, + 'attr' => ['class' => 'select2'], + 'label' => 'Location type', + 'multiple' => false, + ]) ->setAllowedTypes('multiple', ['bool']); } - public function getBlockPrefix(): string + public function getParent(): string { - return 'select2_location_type_type'; + return EntityType::class; } } diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php b/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php index 8fb100441..3c1e51e68 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php @@ -16,7 +16,6 @@ use Chill\MainBundle\Repository\LocationRepository; use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class Select2UserLocationType extends AbstractType @@ -31,10 +30,10 @@ class Select2UserLocationType extends AbstractType $this->locationRepository = $locationRepository; } - public function buildForm(FormBuilderInterface $builder, array $options) + public function configureOptions(OptionsResolver $resolver) { - $builder - ->add('currentLocation', EntityType::class, [ + $resolver + ->setDefaults([ 'class' => Location::class, 'choices' => $this->locationRepository->findByPublicLocations(), 'choice_label' => function (Location $entity) { @@ -44,24 +43,15 @@ class Select2UserLocationType extends AbstractType }, 'placeholder' => 'Pick a location', 'required' => false, - 'label' => $options['label'], - 'label_attr' => $options['label_attr'], - 'multiple' => $options['multiple'], 'attr' => ['class' => 'select2'], - ]); - } - - public function configureOptions(OptionsResolver $resolver) - { - $resolver - ->setDefault('label', 'Current location') - ->setDefault('label_attr', []) - ->setDefault('multiple', false) + 'label' => 'Current location', + 'multiple' => false, + ]) ->setAllowedTypes('multiple', ['bool']); } - public function getBlockPrefix(): string + public function getParent(): string { - return 'select2_user_location_type'; + return EntityType::class; } } diff --git a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php new file mode 100644 index 000000000..f1ef83a9f --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php @@ -0,0 +1,26 @@ +add('currentLocation', Select2UserLocationType::class); + } +} + diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php index 1365757c7..b53960cad 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php @@ -15,7 +15,6 @@ use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class Select2SocialActionType extends AbstractType @@ -27,33 +26,25 @@ class Select2SocialActionType extends AbstractType $this->actionRender = $actionRender; } - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder->add('social_actions', EntityType::class, [ - 'class' => SocialAction::class, - 'choice_label' => function (SocialAction $sa) { - return $this->actionRender->renderString($sa, []); - }, - 'placeholder' => 'Pick a social action', - 'required' => false, - 'label' => $options['label'], - 'label_attr' => $options['label_attr'], - 'multiple' => $options['multiple'], - 'attr' => ['class' => 'select2'], - ]); - } - public function configureOptions(OptionsResolver $resolver) { $resolver - ->setDefault('label', 'Social actions') - ->setDefault('label_attr', []) - ->setDefault('multiple', false) + ->setDefaults([ + 'class' => SocialAction::class, + 'choice_label' => function (SocialAction $sa) { + return $this->actionRender->renderString($sa, []); + }, + 'placeholder' => 'Pick a social action', + 'required' => false, + 'attr' => ['class' => 'select2'], + 'label' => 'Social actions', + 'multiple' => false, + ]) ->setAllowedTypes('multiple', ['bool']); } - public function getBlockPrefix(): string + public function getParent(): string { - return 'select2_social_action_type'; + return EntityType::class; } } diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php index 04b2f723d..aeaeafe34 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php @@ -15,7 +15,6 @@ use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class Select2SocialIssueType extends AbstractType @@ -27,33 +26,25 @@ class Select2SocialIssueType extends AbstractType $this->issueRender = $issueRender; } - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder->add('social_issues', EntityType::class, [ - 'class' => SocialIssue::class, - 'choice_label' => function (SocialIssue $si) { - return $this->issueRender->renderString($si, []); - }, - 'placeholder' => 'Pick a social issue', - 'required' => false, - 'label' => $options['label'], - 'label_attr' => $options['label_attr'], - 'multiple' => $options['multiple'], - 'attr' => ['class' => 'select2'], - ]); - } - public function configureOptions(OptionsResolver $resolver) { $resolver - ->setDefault('label', 'Social issues') - ->setDefault('label_attr', []) - ->setDefault('multiple', false) + ->setDefaults([ + 'class' => SocialIssue::class, + 'choice_label' => function (SocialIssue $si) { + return $this->issueRender->renderString($si, []); + }, + 'placeholder' => 'Pick a social issue', + 'required' => false, + 'attr' => ['class' => 'select2'], + 'label' => 'Social issues', + 'multiple' => false, + ]) ->setAllowedTypes('multiple', ['bool']); } - public function getBlockPrefix(): string + public function getParent(): string { - return 'select2_social_issue_type'; + return EntityType::class; } }