src/Form/EventListener/AddCompaniaFieldSubscriber.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Form\EventListener;
  3. use Symfony\Component\Form\FormEvent;
  4. use Symfony\Component\Form\FormEvents;
  5. use Symfony\Component\Form\FormFactoryInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Doctrine\ORM\EntityRepository;
  8. use App\Entity\Reports;
  9. class AddCompaniaFieldSubscriber implements EventSubscriberInterface
  10. {
  11.    /* private $factory;
  12.     public function __construct(FormFactoryInterface $factory)
  13.     {
  14.         $this->factory = $factory;
  15.     }*/
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             FormEvents::PRE_SET_DATA => 'onPreSetData',
  20.             FormEvents::PRE_SUBMIT   => 'onPreSubmit',
  21.         ];
  22.     }
  23.     private function addCompaniaForm($form$estado//$province)
  24.     {
  25.         $form->add($this->factory->createNamed('compania','entity'null, array(
  26.             'class'         =>Reports::class,
  27.             'empty_value'   => 'Compania',
  28.             'auto_initialize' => false,
  29.             'mapped'        => false,
  30.             'property' => 'fkcompania',
  31.             'query_builder' => function (EntityRepository $repository) use ($estado){//($province) {
  32.                 $qb $repository->createQueryBuilder('c')
  33.                     //->innerJoin('c.estado', 'estado')
  34.                 ;
  35.                 //if ($province instanceof Provincia) {
  36.                 if ($estado instanceof Reports) {
  37.                     $qb->where('c.estado = :estado')
  38.                         ->setParameter('estado'$estado);
  39.                 }/* else {
  40.                     $qb->where('provincia.provincia = :provincia')
  41.                         ->setParameter('provincia', null);
  42.                 }*/
  43.                 return $qb;
  44.             }
  45.         )));
  46.     }
  47.     public function preSetData(FormEvent $event)
  48.     {
  49.         $data $event->getData();
  50.         $form $event->getForm();
  51.         if (null === $data) {
  52.             return;
  53.         }
  54.         $estado = ($data->getSiniestroId()) ? $data->getSiniestroId()->getEstado() : null ;
  55.         $this->addCompaniaForm($form$estado);
  56.     }
  57.     public function preBind(FormEvent $event)
  58.     {
  59.         $data $event->getData();
  60.         $form $event->getForm();
  61.         if (null === $data) {
  62.             return;
  63.         }
  64.         $province array_key_exists('fkprovincia'$data) ? $data['fkprovincia'] : null;
  65.         $this->addCityForm($form$province);
  66.     }
  67. }