<?php
namespace App\Form\EventListener;
use App\Entity\FederalEntity;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Doctrine\ORM\EntityRepository;
use Shapecode\Bundle\HiddenEntityTypeBundle\Form\Type\HiddenEntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use App\Entity\Locality;
class AddLocalityFieldSubscriber implements EventSubscriberInterface
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
private function addField(FormInterface $form, $locality)
{
$localityName = ($locality) ? $locality->getName() : "";
$form
->add('locality', HiddenEntityType::class, [
'help' => 'Escriba las primeras 3 letra del nombre de la localidad',
'class' => Locality::class,
'data' => $locality,
])
->add('localityName', TextType::class, [
'help' => 'Escriba las primeras 3 letra del nombre de la localidad y selecciona de la lista.',
'mapped' => false,
'data' => $localityName,
'label' => 'Localidad',
]);
}
public function onFormPreSetData(FormEvent $event)
{
$data= $event->getData();
$form = $event->getForm();
// /** @var $documentType DocumentType */
// $documentType =
// $documents = (!$documentType) ? [] : $documentType->getDocuments();
$locality = (!$data || null === $data->getId()) ? null : $data->getLocality();
$this->addField($form, $locality);
}
public function onFormPostSubmit(FormEvent $event)
{
$data= $event->getData();
$form = $event->getForm();
// $documentTypeId = $data['documentType'];
// $documentType = $this->em->getRepository(DocumentType::class)->find($documentTypeId);
// $documents = $documentType->getDocuments();
// $this->addField($form, $documentType, $documents);
}
public static function getSubscribedEvents()
{
return [
'form.pre_set_data' => 'onFormPreSetData',
FormEvents::PRE_SUBMIT => 'onFormPostSubmit',
];
}
}