<?php
namespace App\Form\EventListener;
use App\Entity\Groupe;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class AddGroupeFieldListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
FormEvents::POST_SUBMIT => 'onPostSubmit',
];
}
public function onPostSubmit(FormEvent $event)
{
$form = $event->getForm();
$academique = $form->getData();
$form->getParent()->add('groupe',EntityType::class, [
'class' => Groupe::class,
'attr' => [
'data-controller' => 'tom',
'required' => true
],
'placeholder' => '-- choisir une option --',
'choices' => $academique? $academique->getGroupes(): [],
'auto_initialize' => false
]);
}
}