2

I have a form that has location field. Creating a ChoiceType class with dropdown and for options I have defined all the locations in config.yml as parameters.

config.yml:

parameters:
    locations:[location1, location2, location3]

Controller Form Field:

$form = $this->createFormBuilder($abc)
    ->add('location', ChoiceType::class, array(
        'label' => 'Select Location', 
        'choices'=> $this->getParameter('locations'),
    ))
    ->getForm();

This is displaying array location i.e. 0 for location1, 1 for location2 etc, But I want to display array values.

Other things I tried, placing this parameters in a array and then trying array_value options. I got to display location by:

$arrayincontroller=$this->getParameter('locations')

and in form:

->add('location', ChoiceType:class, array('label' => 'Select Location', 'choices'=> array($arrayincontroller[0] => $arrayincontroller[0])

but don't want to write each and every location like this. Is there any better method to pass values to form. Tried to include foreach but guess I can't do that.

1 Answer 1

1

You can define the parameters as array as follow:

parameters:
    locations:
        location1: location1
        location2: location2
        location3: location3

Retrieve always as:

$arrayincontroller=$this->getParameter('locations')

But use as follow:

->add('location', ChoiceType:class, array('label' => 'Select Location', 'choices'=> $arrayincontroller);

Hope this help

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any disadvantage of doing ->add('location', ChoiceType:class, array('label' => 'Select Location', 'choices'=> $this->getParameter('locations');
Hi @HarshulR yes no problem to do directly has you comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.