1

I have applied the example in the symfony documentation to reduce Code Duplication with "inherit_data".

http://symfony.com/doc/current/form/inherit_data_option.html

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // ...

    $builder->add('foo', LocationType::class, array(
        'data_class' => 'AppBundle\Entity\Company'
    ));
}

It works good but when I use this exemple with a search form with GET method I get an url like this :

foo%5Baddress%5D=some+address&foo%5Bzipcode%5D=5000&foo%5Bcity%5D=paris&foo%5Bcountry%5D=france

and i'd like the url to be like this :

address=some+address&zipcode=5000&city=paris&country=france

How can I do that ?

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
hous
  • 2,577
  • 2
  • 27
  • 66

1 Answers1

3

You want a flat (non-nested) form field name. If you want to read the technical details, see this issue and this pull request.

I found some examples here. If you are using Symfony 2, this example would help too. I found two related questions.

Solution 1: Create the form using createNamed method.

Create the form using createNamed method and set the first parameter ($name) to null:

$form = $this->get('form.factory')->createNamed(null, new MyFormType(), $dataObject, $formOptions);

Solution 2: change your FormType

Alternatively, you can use the getBlockPrefix method of your FormType to set the name to null

class MyFormType extends AbstractType
{
    ...

    /**
     * This will remove formTypeName from the form
     * @return null
     */
    public function getBlockPrefix() {
        return null;
    }
}
Community
  • 1
  • 1
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
  • Thanks but this works good with the baseFormType but not with the child form "LocationType". – hous Sep 13 '16 at 09:41