src/EventSubscriber/Form/AddAdresseCongresSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Form;
  3. use App\Entity\Adresse;
  4. use App\Entity\AdresseType;
  5. use App\Entity\Reseau;
  6. use App\Entity\User;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\ORM\EntityRepository;
  9. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Form\Extension\Core\Type\DateType;
  12. use Symfony\Component\Form\FormError;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\Validator\Constraints\NotNull;
  16. class AddAdresseCongresSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var EntityManagerInterface
  20.      */
  21.     private $entityManager;
  22.     public function __construct(EntityManagerInterface $entityManager)
  23.     {
  24.         $this->adresseCongres = [AdresseType::ADRESSE_CONGRES_NATIONAUXAdresseType::ADRESSE_CONGRES_REGIONAUX];
  25.         $this->entityManager $entityManager;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             FormEvents::PRE_SET_DATA => 'preSetData',
  31.             FormEvents::PRE_SUBMIT => 'preSubmit',
  32.         ];
  33.     }
  34.     public function preSetData(FormEvent $event)
  35.     {
  36.         $data $event->getData();
  37.         $form $event->getForm();
  38.         $adresseType $data->getAdresseType();
  39.         if($adresseType){
  40.             $this->addField($form$adresseType);
  41.         }
  42.     }
  43.     public function preSubmit(FormEvent $event)
  44.     {
  45.         $data $event->getData();
  46.         $form $event->getForm();
  47.         if(array_key_exists("adresseType"$data)) {
  48.             $adresseTypeID $data["adresseType"];
  49.             $adresseType $this->entityManager->getRepository(AdresseType::class)->findOneBy(['id'=>$adresseTypeID]);
  50.             if($adresseType){
  51.                 $this->addField($form$adresseType);
  52.             }else{
  53.                 $form->get('adresseType')->addError(new FormError('Type d\'adresse invalide'));
  54.             }
  55.         }
  56.     }
  57.     private function addField($form$adresseType)
  58.     {
  59.         if (in_array($adresseType->getCode(), $this->adresseCongres)) {
  60.             $form->add('visibleForVM');
  61.             $form->add('dateDVisible'DateType::class,
  62.                 array(
  63.                     "required" => false,
  64.                     "label" => "Date de début de visibilité",
  65.                     'widget' => 'single_text',
  66.                     'format' => 'dd/MM/yyyy',
  67.                     // 2. Disable HTML5 option
  68.                     // https://ourcodeworld.com/articles/read/1182/how-to-solve-symfony-5-exception-cannot-use-the-format-option-of-symfony-component-form-extension-core-type-datetype-when-the-html5-option-is-enabled
  69.                     'html5' => false
  70.                     "attr" =>
  71.                         array(
  72.                             "placeholder" => "Date de début de visibilité",
  73.                             "class" => "datetimepicker",
  74.                         )
  75.                 )
  76.             );
  77.             $form->add('dateFVisible'DateType::class,
  78.                 array(
  79.                     "required" => false,
  80.                     "label" => "Date de fin de visibilité",
  81.                     'widget' => 'single_text',
  82.                     'format' => 'dd/MM/yyyy',
  83.                     // 2. Disable HTML5 option
  84.                     // https://ourcodeworld.com/articles/read/1182/how-to-solve-symfony-5-exception-cannot-use-the-format-option-of-symfony-component-form-extension-core-type-datetype-when-the-html5-option-is-enabled
  85.                     'html5' => false
  86.                     "attr" =>
  87.                         array(
  88.                             "placeholder" => "Date de fin de visibilité",
  89.                             "class" => "datetimepicker",
  90.                         )
  91.                 )
  92.             );
  93.         }else{
  94.             $form->remove('visibleForVM');
  95.             $form->remove('dateDVisible');
  96.             $form->remove('dateFVisible');
  97.         }
  98.     }
  99. }