src/FormSubscriber/RFEnvioCorrespondenciaSubscriber.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 RFEnvioCorrespondenciaSubscriber 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.         if (isset($data['cliente'])) {
  25.             $idCliente $data['cliente'];
  26.             $cliente $this->entityManager->getRepository(\App\Entity\TerEmpresaCliente::class)->find($idCliente);
  27.             $data['direccionDestino'] = $cliente->getDireccion();
  28.             $event->setData($data);
  29.         }
  30.     }
  31. }