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) {
if($c->getEmail()){
$emails[]= $c->getEmail();
}
}
if(isset($emails)){
$email = (new TemplatedEmail())
->from($contact->get('email')->getData())
->to(...$emails)
->subject('contact')
->htmlTemplate('emails/convocation_match.html.twig')
->context([
'catadherent' =>$catadherent,
'mail' => $contact->get('email')->getData(),
'mailTo'=>$contact->get('emailTo')->getData(),
'team'=> $team->getName(),
'convocation_date'=>$contact->get('convocation_date')->getData(),
'club_adverse'=>$contact->get('club_adverse')->getData(),
'rendez_vous_date'=>$contact->get('rendez_vous_date')->getData(),
'lieu'=>$contact->get('lieu')->getData(),
'match_date'=>$contact->get('match_date')->getData(),
'stade'=>$contact->get('stade')->getData(),
]);
$mailer->send($email);
$this->AddFlash(
'success',
"Votre email a bien été envoyé !"
);
//return $this->redirectToRoute('adherent_show',['id' => $adherent->getId()]);
}
else{
$this->AddFlash(
'danger',
"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