7

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)?

3 Answers 3

26

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

6 Comments

Is it possible to do something like this when using FormTypes as services? I don't think you can access createNamedBuilder in that context and using FormType::getName() { return ''; } causes problems referencing the FormType as a service.
Of course you can access createNamedBuilder(), you just need to get access to the form factory :)
$builder->getFormFactory()... who knew? Thanks!
FormFactory -> createNamed could be used to get a Form instance instead of a builder
@caponica have you found a solution when using form as service ? I am stuck on this!
|
7

old thread, but worth mentioning that symfony 3 ignores getName entirely.

However, you can do the same with getBlockPrefix if you need the form name to be blank.

public function getBlockPrefix() {
    return null;
}

this will result in form fields being named without a prefix.

Comments

2

using return null; in your implementation of AbstractType::getName seems to have the same effect these days.

2 Comments

But if formtype returns null, is not possible to generate formType with dependency injection
Cannot use this approach with DI in general. Returning null is equivalent to the accepted answer, see for yourself.

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.