Symfony 5 - Custom ChoiceType with TextType
Asked Answered
A

1

0

In Symfony 5, I have a ChoiceType in a form and I have 2 choices. I want to put an "other" choice where you can write what you want. I try to put a TextType in the choices.

Thanks in advance

->add('projectType', ChoiceType::class,[
                'label' => 'Votre projet concerne : ',
                'choices' => [
                    'Maison' => 'Maison',
                    'Appartement' => 'Appartement',
                ]
            ])

Do you know how to add a TextType to my ChoiceType?

Apophasis answered 21/8, 2020 at 8:16 Comment(1)
Not sure you can do that. but you can display an other text field when the user select "other"Strath
U
1

It's not easy to do, but you can do it with a customs form type field and a Twig theme:

1.Create a new field type like ChoiceInputType. That class should contain two field types, ChoiceType and TextType.

/**
 * Class ChoiceInputType
 */
class ChoiceInputType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add(
                'choiceType',
                ChoiceType::class,
                [
                    'required' => false,
                    'choices'  => $options['choices'],
                    'label' => 'Make your choice...',
                ]
            )
            ->add(
                'choiceInput',
                TextType::class,
                [
                    'required' => false,
                    'label'    => false,
                    'attr'     => [
                        'placeholder' => 'Other choice...',
                    ],
                ]
            );
    }

    /**
     * {@inheritdoc}
     */
    public function buildView(FormView $view, FormInterface $form, array $options): void
    {
        $view->vars['choices'] = $options['choices'];
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setRequired(['choices']);

        $resolver->setDefaults(
            [
                'choices' => [],
            ]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getName(): string
    {
        return 'choiceInputType';
    }
}

2.Create a twig theme on your new custom field type and organize it as you want.

{% block choiceInputType_widget %}
    ... custom here ...
{% endblock %}

3.Use your new ChoiceInputTypeinside your form.

$builder->add(
     'choiceInputType',
     ChoiceInputType::class,
         [
            'mapped'  => false,
            'block_prefix' => 'choiceInputType',
            'label'   => false,
            'choices' => [
                'test 1' => 1,
                'test 2' => 2,
                'test 3' => 3,
             ]
          ]
  );

Here some links to other questions similar to this thread that can help you:

Unwholesome answered 21/8, 2020 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.