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:
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,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;
}
}