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
This commit is contained in:
Mathieu Jaumotte 2022-10-16 13:21:03 +02:00
parent 5c6068e8a5
commit a46c85d66c
6 changed files with 75 additions and 86 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Form;
use Chill\MainBundle\Form\Type\Select2UserLocationType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class UserCurrentLocationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('currentLocation', Select2UserLocationType::class);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}