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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author Mehrez Labidi
  5.  */
  6. namespace App\Form\Type\DepotAnnonce;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use App\Validator\Constraints\Annee;
  9. use App\Validator\Constraints\Montant;
  10. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\FormEvent;
  13. use Symfony\Component\Form\FormEvents;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use Symfony\Component\Validator\Constraints\NotBlank;
  16. class ValidatorElementsChiffresN implements EventSubscriberInterface {
  17.     /**
  18.      * @return string[]
  19.      */
  20.     public static function getSubscribedEvents(): array {
  21.         return [
  22.             FormEvents::PRE_SET_DATA => 'onPreSetData',
  23.             FormEvents::PRE_SUBMIT => 'onPreSubmit'
  24.         ];
  25.     }
  26.     /**
  27.      * 
  28.      * @param FormEvent $event
  29.      * @return void
  30.      */
  31.     public function onPreSetData(FormEvent $event): void {
  32.         $this->validation($event);
  33.     }
  34.     /**
  35.      * 
  36.      * @param FormEvent $event
  37.      * @return void
  38.      */
  39.     public function onPreSubmit(FormEvent $event): void {
  40.         $this->validation($event);
  41.     }
  42.     /**
  43.      * 
  44.      * @param type $event
  45.      */
  46.     private function validation($event) {
  47.         $user $event->getData();
  48.         $form $event->getForm();
  49.         // if ((empty($user['EBE_n'])) && (empty($user['resultat_net_n'])) && (empty($user['resultat_exploitation_n']))) {
  50.         // Vérifier si les champs sont null ou chaîne vide, mais pas zéro
  51.         
  52.         $ebeEmpty = !isset($user['EBE_n']) || $user['EBE_n'] === '' || $user['EBE_n'] === null;
  53.         $resultatNetEmpty = !isset($user['resultat_net_n']) || $user['resultat_net_n'] === '' || $user['resultat_net_n'] === null;
  54.         $resultatExploitationEmpty = !isset($user['resultat_exploitation_n']) || $user['resultat_exploitation_n'] === '' || $user['resultat_exploitation_n'] === null;
  55.         if ($ebeEmpty && $resultatNetEmpty && $resultatExploitationEmpty) {
  56.             $form->add('resultat_net_n',
  57.                     TextType::class,
  58.                     [
  59.                         "attr" => ["autocomplete" => "off"],
  60.                         "constraints" => [
  61.                             new Montant(),
  62.                             new NotBlank(["message" => "Le champ Resultat net n est obligatoire"])
  63.                         ]
  64.                     ]
  65.             );
  66.             $form->add('EBE_n',
  67.                     TextType::class,
  68.                     [
  69.                         "attr" => ["autocomplete" => "off"],
  70.                         "constraints" => [
  71.                             new Montant(),
  72.                             new NotBlank(["message" => "Le champ EBE n est obligatoire"])
  73.                         ]
  74.                     ]
  75.             );
  76.             $form->add('resultat_exploitation_n'TextType::class,
  77.                     [
  78.                         "attr" => ["autocomplete" => "off"],
  79.                         "constraints" => [
  80.                             new Montant(),
  81.                             new NotBlank(["message" => "Le champ Resultat Exploitation n est obligatoire"])
  82.                         ]
  83.                     ]
  84.             );
  85.         } else {
  86.             $form->add('EBE_n'TextType::class, ["constraints" => [new Montant()], "attr" => ["autocomplete" => "off"]]);
  87.             $form->add('resultat_exploitation_n'TextType::class, ["constraints" => [new Montant()], "attr" => ["autocomplete" => "off"]]);
  88.             $form->add('resultat_net_n'TextType::class, ["constraints" => [new Montant()], "attr" => ["autocomplete" => "off"]]);
  89.         }
  90.     }
  91. }