Mohammad Fareed
Mohammad Fareed

Reputation: 1972

How get field value in form builder in Symfony

How to get field value in form builder in Symfony. I have 2 Dropdowns in the form I want to should the related option in Dropdown2 based on Dropdown1 in when the Page is Opening.

Here is My Form

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\DataEvent;
use C2Educate\ToolsBundle\Entity\Students;
public function buildForm(FormBuilder $builder, array $options) {

Field 1:

$builder->add('leadSource', 'entity', array( 
        'label' => 'How did you hear about C2?   Source ',

        'class' => 'C2EducateToolsBundle:LeadSources',
        'query_builder' => function($repo)  {
            return $repo->createQueryBuilder('p')->orderBy('p.sort_order', 'ASC');
        },
        'property' => 'name',
        'empty_value' => 'Select'

    ));
$leadSource = 1;

$leadSource = 1; - it works when I assign value statically, but I want to get the value of "leadSource" and assign it to $leadSource

I want to get the leadSource and pass it to leadSourceSub query

Field 2:

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (DataEvent $event) { 
    $form = $event->getForm();
    $entity = $event->getData();
    $leadSource = $entity->getLeadSourceID();
    $form->add('leadSourceSub', 'C2Educate\ToolsBundle\Entity\Students', array(
            'label' => ' Source Detail ',
            'required' => true,
            'class' => 'C2EducateToolsBundle:LeadSourceSubs',
            'query_builder' => function($repo) use ($leadSource) {
                return $repo->createQueryBuilder('p')
                        ->where('p.lead_source_id =:leadSource')
                        ->setParameter('leadSource', $leadSource)
                        ->orderBy('p.sort_order', 'ASC');
            },
            'property' => 'name',
            'empty_value' => 'Select'
        ));
    });

Upvotes: 1

Views: 5919

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

You cannot get form data from $builder, because... it's a form builder, not a form. It doesn't contain any data yet.

To make this work you need to make use of FormEvents. In this case, you probably will need FormEvents::PRE_SET_DATA event listener.

    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { 
        $form = $event->getForm();
        // in your case it's C2EducateToolsBundle:LeadSourceSubs
        $entity = $event->getData();
        $leadSource = $entity->getLeadSource();

        // adding this field again will override it.
        $form->add('leadSourceSub', 'entity', array(
                'label' => ' Source Detail ',
                'required' => true,
                'class' => 'C2EducateToolsBundle:LeadSourceSubs',
                'query_builder' => function($repo) use ($leadSource) {
                    return $repo->createQueryBuilder('p')
                            ->where('p.lead_source_id =:leadSource')
                            ->setParameter('leadSource', $leadSource)
                            ->orderBy('p.sort_order', 'ASC');
                },
                'property' => 'name',
                'empty_value' => 'Select'
            ));
        }
    });

Please note that this code is not tested and may need some validation like to check if $entity is what you expect it to be in any case.

Upvotes: 4

Related Questions