TeoM
TeoM

Reputation: 105

How to get the data from a field displayed with form events in symfony?

I am using Symfony 3.0

I have a country->region->locality->medical center form type. All these fields are interdependent using ajax.

I made four Event subscribers, each of them have onPreSetData and onPreSubmit. Everything is working well until the moment when I wish to process the submited data in my controller.

Being not mapped fields I though I could access the submited value with $form->get("field_name")->getData();

But this is null for these fields.

How can I access the values submited in these fields?

This is how I added the fields:

->add('medical_center', MedicalCenterType::class, array(
            'mapped' => false
        ));

Medical Center Type:

class MedicalCenterType extends AbstractType
{

/** @var  EntityManager */
protected $em;
/**
 * @param mixed $em
 * @return $this
 */
public function setEntityManager($em)
{
    $this->em = $em;
    return $this;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->addEventSubscriber(new AddCountryFieldSubscriber($this->em, 38))
        ->addEventSubscriber(new AddRegionFieldSubscriber($this->em,  38))
        ->addEventSubscriber(new AddLocalityFieldSubscriber($this->em))
        ->addEventSubscriber(new AddMedicalCenterFieldSubscriber($this->em))
    ;
}
}

And the subscriber:

class AddMedicalCenterFieldSubscriber implements EventSubscriberInterface
    {
        /** @var  EntityManager */
        protected $em;

        /** @var  int */
        protected $locality;

        /** @var  int */
        public $medical_center;

        public function __construct(EntityManager $em, $locality = null)
        {
            $this->em = $em;
            $this->locality = $locality;
        }

        public static function getSubscribedEvents()
        {
            return array(
                FormEvents::PRE_SET_DATA => 'onPreSetData',
                FormEvents::PRE_SUBMIT => 'onPreSubmit'
            );
        }

        public function onPreSetData(FormEvent $event)
        {
           $this->formModifier($event);
        }

        public function onPreSubmit(FormEvent $event)
        {
            $data = $event->getData();
            $this->locality = $data['locality'];
            $this->formModifier($event);
        }

        private function formModifier(FormEvent $event)
        {
            $form = $event->getForm();

            $form->add('medical_center', EntityType::class, array(
                'placeholder' => 'Select a locality first',
                'class' => 'WebsiteBundle:MedicalCenters',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('mc')
                        ->where('mc.localities = :locality')
                        ->orderBy('mc.name')
                        ->setParameter('locality', $this->locality);

                },
                'constraints' => new NotBlank(),
                'choice_label' => 'name',
                'mapped'=>false
            ));
        }

    }

Upvotes: 0

Views: 1813

Answers (1)

TeoM
TeoM

Reputation: 105

I found the solution, it was:

$form->get('medical_center')->get('medical_center')->getData();

Upvotes: 1

Related Questions