Akhmed
Akhmed

Reputation: 1179

Symfony 2 forms - pass data to select without data class (no entity)

Suppose we have form in our controller:

$form = $this->createForm(new OurFormType());

Here is builder method of OurFormType class

public function buildForm(FormBuilderInterface $builder, array $options) {
     $builder
        ->add('list','choice')
        ->add('agency','text')
        ->add('type','hidden');
}

We have no data_class, no entity used at all. We can pass the data to the form using other way. The thing we interested in is list field, that has choice type. This type is used to create select list on symfony 2 forms.

Here we came to the task. And the task is to fill that list with data, that get in controller. Suppose it is associative array. So we can provide our modified controller action:

$listData = array('key1'=>'val1', 'key2' => 'val2');
$form = $this->createForm(new OurFormType());

return $this->render('UMDOurBundle:Test:index.html.twig',
    array(
        'form' => $form->createView()
    ));

I need to insert that array to the list field in my controller, after I pass object to $form variable. Something like

$form->get('list',array('choices'=>$listData));

Is there something like that in symfony 2 forms?

Upvotes: 2

Views: 4544

Answers (1)

Acyra
Acyra

Reputation: 16034

Check the answer on Symfony 2 - how to pass data to formBuilder? for the techinque to pass values to a FormType class using the constructor.

Upvotes: 1

Related Questions