src/FormSubscriber/SegIngresoVisitanteSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. // src/Form/EventListener/AddNameFieldSubscriber.php
  3. namespace App\FormSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\FormEvent;
  7. use Symfony\Component\Form\FormEvents;
  8. use Symfony\Component\Form\Form;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. class SegIngresoVisitanteSubscriber implements EventSubscriberInterface {
  11.     private $entityManager;
  12.     public function __construct(
  13.             EntityManagerInterface $entityManager
  14.     ) {
  15.         $this->entityManager $entityManager;
  16.     }
  17.     public static function getSubscribedEvents(): array {
  18.         // Tells the dispatcher that you want to listen on the form.pre_set_data
  19.         // event and that the preSetData method should be called.
  20.         return [FormEvents::PRE_SUBMIT => 'preSubmit'];
  21.     }
  22.     public function preSubmit(FormEvent $event): void {
  23.         $data $event->getData();
  24.         $form $event->getForm();
  25.         //data es un arreglo con los valores establecidos por el usuario en el form.
  26.         //como $data contiene el pais seleccionado por el usuario al enviar el formulario,
  27.         // usamos el valor de la posicion $data['country'] para filtrar el sql de los estados
  28.         if($data['numeroIdentificacion']){
  29.             $visitante $this->entityManager->getRepository(\App\Entity\SegVisitante::class)->findOneBy(['numeroIdentificacion' => $data['numeroIdentificacion']]);
  30.             if ($visitante) {
  31.                 $data['tipoDocumento'] = $visitante->getTipoDocumento();
  32.                 $data['motivoVisita'] = $visitante->getMotivoVisita();
  33.                 $data['fechaVisita'] = $visitante->getFechaVisita();
  34.                 $data['usuarioAtiende'] = $visitante->getUsuarioAtiende();
  35.                 $data['condicionesEspeciales'] = $visitante->getCondicionesEspeciales();
  36.                 $data['observaciones'] = $visitante->getObservaciones();
  37.             }
  38.         }
  39.         $event->setData($data);
  40.     }
  41. }