<?php
// src/Form/EventListener/AddNameFieldSubscriber.php
namespace App\FormSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Form;
use Doctrine\ORM\EntityManagerInterface;
class SegIngresoVisitanteSubscriber implements EventSubscriberInterface {
private $entityManager;
public function __construct(
EntityManagerInterface $entityManager
) {
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array {
// Tells the dispatcher that you want to listen on the form.pre_set_data
// event and that the preSetData method should be called.
return [FormEvents::PRE_SUBMIT => 'preSubmit'];
}
public function preSubmit(FormEvent $event): void {
$data = $event->getData();
$form = $event->getForm();
//data es un arreglo con los valores establecidos por el usuario en el form.
//como $data contiene el pais seleccionado por el usuario al enviar el formulario,
// usamos el valor de la posicion $data['country'] para filtrar el sql de los estados
if($data['numeroIdentificacion']){
$visitante = $this->entityManager->getRepository(\App\Entity\SegVisitante::class)->findOneBy(['numeroIdentificacion' => $data['numeroIdentificacion']]);
if ($visitante) {
$data['tipoDocumento'] = $visitante->getTipoDocumento();
$data['motivoVisita'] = $visitante->getMotivoVisita();
$data['fechaVisita'] = $visitante->getFechaVisita();
$data['usuarioAtiende'] = $visitante->getUsuarioAtiende();
$data['condicionesEspeciales'] = $visitante->getCondicionesEspeciales();
$data['observaciones'] = $visitante->getObservaciones();
}
}
$event->setData($data);
}
}