src/Form/Type/DepotAnnonce/ValidatorTypePartenaire.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2.  
  3. /**
  4.  * @Description: validation dynamique depot annonce  partenaire : type_partenaire  personne_morale ou non 
  5.  * @author Mehrez Labidi
  6.  */
  7. namespace App\Form\Type\DepotAnnonce;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use App\Validator\Constraints\Annee;
  10. use App\Validator\Constraints\Montant;
  11. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. class ValidatorTypePartenaire implements EventSubscriberInterface {
  18.     /**
  19.      * @return string[]
  20.      */
  21.     public static function getSubscribedEvents(): array {
  22.         return [
  23.             FormEvents::PRE_SET_DATA => 'onPreSetData',
  24.             FormEvents::PRE_SUBMIT => 'onPreSubmit'
  25.         ];
  26.     }
  27.     /**
  28.      * 
  29.      * @param FormEvent $event
  30.      * @return void
  31.      */
  32.     public function onPreSetData(FormEvent $event): void {
  33.         $this->validation($event);
  34.     }
  35.     /**
  36.      * 
  37.      * @param FormEvent $event
  38.      * @return void
  39.      */
  40.     public function onPreSubmit(FormEvent $event): void {
  41.         $this->validation($event);
  42.     }
  43.     /**
  44.      * 
  45.      * @param type $event
  46.      */
  47.     private function validation($event) {
  48.         $user $event->getData();
  49.         $form $event->getForm();
  50.         if (!empty($user['type_partenaire'])) {
  51.             if ($user['type_partenaire'] === "personne_morale") {
  52.                 $form->add('nb_personnes_min'TextType::class,
  53.                         [
  54.                             "required" => true,
  55.                             "constraints" => [
  56.                                 new NotBlank(["message" => "Le champ nb personnes min est obligatoire"]),
  57.                                 new Assert\Length(['max' => 100'maxMessage' => "Le champ nb personnes min est trop long"])
  58.                             ], "attr" => ["autocomplete" => "off"]
  59.                 ]);
  60.                 $form->add('nb_personnes_max'TextType::class,
  61.                         [
  62.                             "required" => true,
  63.                             "constraints" => [
  64.                                 new NotBlank(["message" => "Le champ nb personnes max est obligatoire"]),
  65.                                 new Assert\Length(['max' => 100'maxMessage' => "Le champ nb personnes max est trop long"])
  66.                             ], "attr" => ["autocomplete" => "off"]
  67.                 ]);
  68.                 $form->add('ca_min'TextType::class,
  69.                         [
  70.                             "required" => true,
  71.                             "constraints" => [
  72.                                 new NotBlank(["message" => "Le champ ca min est obligatoire"]),
  73.                                 new Assert\Length(['max' => 100'maxMessage' => "Le champ ca min est trop long"])
  74.                             ], "attr" => ["autocomplete" => "off"]
  75.                 ]);
  76.                 $form->add('ca_max'TextType::class,
  77.                         [
  78.                             "required" => true,
  79.                             "constraints" => [
  80.                                 new NotBlank(["message" => "Le champ ca max est obligatoire"]),
  81.                                 new Assert\Length(['max' => 100'maxMessage' => "Le champ ca max est trop long"])
  82.                             ], "attr" => ["autocomplete" => "off"]
  83.                 ]);
  84.             }
  85.         }
  86.     }
  87. }