inject variable from controller to FormType

I am under symfony 4.4.

I would like to know how to pass a variable from my controller to my formType to prepopulate a field. I searched but could not find.

My controller:

 class TeamController extends AbstractController
    public function index($id,Request $request,MailerInterface $mailer)
    $repo = $this->getDoctrine()->getRepository(Team::class);
        $team = $repo->find($id);

           $formcontact = $this->createForm(ConvocationType::class);
           $contact = $formcontact->handleRequest($request);
           if($formcontact->isSubmitted() && $formcontact->isValid()){
           foreach ($team->getAdherents() as $c) {
            $mails[] = $c->getEmail();
            foreach ($contact->get('emailTo')->getData() as $c) {
                $emails[]= $c->getEmail();
               $email = (new TemplatedEmail())
                  'catadherent' =>$catadherent,
                  'mail' => $contact->get('email')->getData(),
                  'team'=> $team->getName(),
                   "Votre email a bien été envoyé !"
               //return $this->redirectToRoute('adherent_show',['id' => $adherent->getId()]);
                    "Votre email n'a pas été envoyé car aucun mail n'a été sélectionné !"
        return $this->render('team/index.html.twig', [
            'team' => $team,
            'team1' =>$team1,
            'catadherent' => $catadherent,
            'adherent' => $adherent,
            'mails' => $mails,
            'formcontact' =>$formcontact->createView()

This is my mail variable that I would like to retrieve in my formType:

public function buildForm(FormBuilderInterface $builder, array $options)
     ->add('emailTo',EntityType::class, [
            'label' => 'Emails sélectionnés',
            'multiple' => true,
            'expanded' => true,
            'class'  => Adherent::class,
            'choice_label' => 'LNAndFn',
            'mapped' => false,
            'choice_attr' => function() {
                return ['checked' => 'checked'];

I would like to remove the entityType and insert in data my variable ‘$ mails’ and keep my checkboxes.

thank you in advance for your help

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *