Symfony2.1 使用带有 GET 方法的表单 [英] Symfony2.1 using form with method GET

查看:28
本文介绍了Symfony2.1 使用带有 GET 方法的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要关于使用带有 method=GET 和干净 URL 空间的 Symfony2.1 表单的帮助.

I need help on using Symfony2.1 forms with method=GET and a clean URL space.

我正在创建一个过滤器",我想在 URL 中设置它,以便人们可以为他们的链接添加书签.

I am creating a "filter" which I'd like to set in the URL so that people can bookmark their links.

所以,代码非常简单:

$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.

但是,当我提交表单时,它会产生非常难看的 GET 参数:

However when I submit the form, it produces very ugly GET parameters:

/app_dev.php/de/event?form%5Bfrom%5D=17.11.2012

这是因为输入的名字当然是form[from]

This is because the input name is of course form[from]

所以为了清理 URL 空间,我给自己做了一个主题:

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 %}

我将 name="{{ full_name }}" 替换为 name="{{ id }}".

这很有效 - 我的 URL 空间更干净:

This works well - my URL space is cleaner:

/app_dev.php/de/event?form_from=17.11.2012

我想我可以接受 - 尽管理想情况下 from=xxx 会更好.这是第一个也是更小的问题.

I guess I could live with that - although ideally from=xxx would be better. That is the first and more minor problem.

第二个问题是我不能再绑定表单了——这很明显,因为参数form"不再设置——form_from"已经取代了它,但是当你进行绑定时,它仍然在期待形式[].

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.

所以,问题是:我应该只使用 form%5Bfrom%5D url,还是有更好的方法来完成所有这些(显然不使用 POST)?

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

推荐答案

您可以将根表单的名称设置为空,那么您的字段名称将只是form.通过

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'
    ));

这篇关于Symfony2.1 使用带有 GET 方法的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆