如何处理Symfony2 Beta上的表单集合? [英] How to deal with Form Collection on Symfony2 Beta?

查看:103
本文介绍了如何处理Symfony2 Beta上的表单集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体User和一个实体Address。用户和地址之间存在一对多关系:

  class User 
{
/ **
* @orm:OneToMany(targetEntity =Address)
* /
protected $ adresses;

[...]

}

我有一个类AddressType和类UserType:

  class UserType extends AbstractType 
{
public function buildForm(FormBuilder $ builder,array $ options)
{
$ builder-> add('addresses','collection',array('type'=> new AddressType()));

}

[...]
}


b $ b

在我的控制器中,我用下面的形式构建表单:

  $ form = $ this-& form.factory') - > create(new UserType()); 

...并创建视图:

  return array('form'=> $ form-> createView()); 

我在我的树枝模板中显示表单字段:

  {{form_errors(form.name)}} 
{{form_label(form.name)}}
{{form_widget }
[...]

好吧。现在,如何显示一个或多个地址的字段? (它不是 {{for_widget(form.adresses.zipcode)}} 也不是 {{for_widget(form.adresses [0] .zipcode)}} ...)



有任何想法吗?

解决方案

这是我在表单模板中执行的操作:

  {{form_errors(form.addresses)}} 

{%for form.addresses%}
< div id ={ 'address%sDivId'| format(loop.index)}}class =userAddressItem>
< h5>地址#{{loop.index}}< / h5>

{{form_errors(address)}}
{{form_widget(address)}}
< / div>
{%endfor%}

我有一个小的action bar,允许用户添加和删除地址。它是一个简单的脚本,使用正确的HTML代码将新的div附加到容器中。对于HTML,我只是使用相同的输出具有Symfony但更新索引。例如,这将是 AddressType 表单的街道输入文本的输出:



< input id =user_addresses_0_streetname =user [addresses] [0] [street]...>





< input id = user_addresses_1_streetname =user [addresses] [1] [street]...>



注意: required =requiredmaxlength =255,但可根据您的需要进行更改。



需要更多的HTML代码来添加一个新的 AddressType 到浏览器的DOM,但这给你的一般想法。



尊敬的,

Matt


I have an entity User and an entity Address. There is a relation One-to-Many between User and Address :

    class User
    {
        /**
        * @orm:OneToMany(targetEntity="Address")
        */
        protected $adresses;

        [...]

    }

I have a class AddressType, and class UserType :

    class UserType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            $builder->add('addresses', 'collection', array('type' => new AddressType()));

        }

        [...]
    }

In my controller, I build form with :

    $form = $this->get('form.factory')->create(new UserType()); 

... and create view with :

    return array('form' => $form->createView());

I display form field in my twig template with :

    {{ form_errors(form.name) }}
    {{ form_label(form.name) }}
    {{ form_widget(form.name) }}
    [...]

Okay. Now, how to display fields for one or more addresses ? (it's no {{ for_widget(form.adresses.zipcode) }} nor {{ for_widget(form.adresses[0].zipcode) }} ...)

Any ideas ?

解决方案

This is how I did it in my form template:

{{ form_errors(form.addresses) }}

{% for address in form.addresses %}
    <div id="{{ 'address%sDivId'|format(loop.index) }}" class="userAddressItem">
        <h5> Address #{{ loop.index }}</h5>

        {{ form_errors(address) }}
        {{ form_widget(address) }}
    </div>
{% endfor %}

And I have a small action bar, driven by jQuery, that lets the user add and remove addresses. It is a simple script appending a new div to the container with the right HTML code. For the HTML, I just used the same output has Symfony but with updated index. For example, this would be the output for the street input text of the AddressType form:

<input id="user_addresses_0_street" name="user[addresses][0][street]" ...>

Then, the next index Symfony will accept is 1 so the new input field you add would look like this:

<input id="user_addresses_1_street" name="user[addresses][1][street]" ...>

Note: The three dots are a remplacement for required="required" maxlength="255" but could change depending on your needs.

You will need more HTML code than that to add a whole new AddressType to the DOM of the browser but this give you the general idea.

Regards,
Matt

这篇关于如何处理Symfony2 Beta上的表单集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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