1
votes

I would like to make a form with "selected" in option to choose a language. By default, I would like to "selected" User's locale option.

(...)
use Symfony\Component\HttpFoundation\Request;
(...)


class VisitType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options, Request $request)
        {

            $locale = $request->getLocale();
            if ($locale == 'fr'){
                $prefLang = "Français";
            }elseif ($locale == 'nl'){
                $prefLang = "Nederlands";
            }elseif ($locale == 'en') {
                 $prefLang = "English";
            }

            $builder->add('langvis', ChoiceType::class, [
                    'label' => 'form.input.langvis',
                    'choices' => [
                        'Français' => 'Français',
                        'Nederlands' => 'Nederlands',
                        'English' => 'English'
                    ],
                    'preferred_choices' => [$prefLang]
                ])
                ->add('save', SubmitType::class, [
                    'label' => 'form.input.send',
                    'attr' => [
                        'class' => 'btn btn-primary form-control'
                    ]
                ])
                ->getForm();
                     ;
        }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Visit::class
        ]);
    }
}

But I have a compile Error:

Declaration of App\Form\VisitType::buildForm(Symfony\Component\Form\FormBuilderInterface $builder, array $options, Symfony\Component\HttpFoundation\Request $request) must be compatible with Symfony\Component\Form\AbstractType::buildForm(Symfony\Component\Form\FormBuilderInterface $builder, array $options)

How can I do that?

1

1 Answers

1
votes

you cannot pass request on buildForm, try using OptionsResolver

class VisitType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $locale = $options["locale"];
        if ($locale == 'fr') {
            $prefLang = "Français";
        } elseif ($locale == 'nl') {
            $prefLang = "Nederlands";
        } elseif ($locale == 'en') {
            $prefLang = "English";
        }

        $builder->add('langvis', ChoiceType::class, [
                    'label' => 'form.input.langvis',
                    'choices' => [
                        'Français' => 'Français',
                        'Nederlands' => 'Nederlands',
                        'English' => 'English'
                    ],
                    'preferred_choices' => [$prefLang]
                ])
                ->add('save', SubmitType::class, [
                    'label' => 'form.input.send',
                    'attr' => [
                        'class' => 'btn btn-primary form-control'
                    ]
                ])
                ->getForm();
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults([
            'data_class' => Visit::class,
            'locale' => 'Français'
        ]);
    }

In controller, when you create form pass locale parameter:

$locale = $request->getLocale();
$formType = 'VisitType';
$entity = 'VisitEntity';
$form = $this->createForm($formType, $entity, array('locale' => $locale));