<?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 GHRetiroCesantiasSubscriber 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();
//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 (isset($data['inversion'])) {
$idBusqueda = $data['inversion'];
$inversion = $this->entityManager->getRepository(\App\Entity\ParInversion::class)->find($idBusqueda);
$documentos = $inversion->getDocumentosAnexar();
$data['documento'] = $documentos;
$event->setData($data);
}
}
}