Symfony2.1 using form with method GET
symfony-2.1, symfony-forms, url
Solution
You can set the name of the root form to empty, then your field name will be just `form`. Do so via
// the first argument to createNamedBuilder() is the name
$form = $this->get('form.factory')->createNamedBuilder(null, 'form', $defaultData)
->add('from', 'date', array(
'required' => false,
'widget' => 'single_text',
'format' => 'dd.MM.yyyy'
));
Problem
I need help on using Symfony2.1 forms with method=GET and a clean URL space. I am creating a "filter" which I'd like to set in the URL so that people can bookmark their links. So, very simply the code: ``` $form = $this->createFormBuilder($defaultData) ->add('from', 'date', array('required' => false, 'widget' => 'single_text', 'format' => 'dd.MM.yyyy')) ``` I render the form widget and all is fine. However when I submit the form, it produces very ugly GET parameters: ``` /app_dev.php/de/event?form%5Bfrom%5D=17.11.2012 ``` This is because the input name is of course `form[from]` So to clean the URL space, I made myself a theme: ``` {% block widget_attributes %} {% spaceless %} id="{{ id }}" name="{{ id }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %} {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %} {% endspaceless %} {% endblock widget_attributes %} ``` where I replaced `name="{{ full_name }}"` with `name="{{ id }}"`. This works well - my URL space is cleaner: ``` /app_dev.php/de/event?form_from=17.11.2012 ``` I guess I could live with that - although ideally `from=xxx` would be better. That is the first and more minor problem. The second problem is that I can't get the form to bind anymore - this is obvious because the parameter "form" is no longer set - "form_from" has replaced it, but when you do a bind it is still expecting form[]. I tried to fix that like this: ``` $fromDate = $this->get('request')->query->get('form_from', null); $request->query->set('form', array('from' => $fromDate); ``` But that doesn't work. I also suspect that I am digging a huge hole of hacks at the moment. So, the question is: should I just live with the `form%5Bfrom%5D` url, or is there a better way to do all of this (without using POST obviously)?