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 ?

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but this works good with the baseFormType but not with the child form "LocationType".

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.