Search code examples
phpformssymfonyformbuilder

Setting name attribute of symfony 2 form field


How to set name attribute of rendered field in symfony 2?

Expected output:

     <input type="text" name="test" value="test" />

Rendering the field this way

     {{ form_widget(form.test, { 'attr': {'name': 'test'} }) }}

definitely does not work.

Output is still

     <input type="text" name="form[test]" value="test" />.

Is there any way to set the name attribute or id attribute dynamically? Thank you.


Solution

  • <input type="text" name="form[test]" value="test" />
    

    already has test as name. Of course there is still the root form name called form in your case. Removing this is not really recommended, because when you read the request to populate form data you can identify the form by its form name.

    Read [Form] Enable empty root form name https://github.com/symfony/symfony/pull/2936