symfony2 中同一实体的表单中的多行 [英] multiple rows in form for the same entity in symfony2

查看:34
本文介绍了symfony2 中同一实体的表单中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含多行的简单表单:

控制器:

公共函数 indexAction(){$repository = $this->getDoctrine()->getRepository('MyBundle:Product');$products = $repository->findAll();foreach ($products as $product) {$forms[] = $this->createForm(new ProductType, $product)->createView();}return $this->render('MBundle:Default:index.html.twig', array('form' => $forms);}

我用树枝渲染:

{% for key, formData in forms %}{{ form_row(formData.id) }}{{ form_row(formData.name) }}{{ form_row(formData.nameEnglish) }}<br clear="all"/>{% 结束为 %}</表单>

当我提交表单时,我的每个输入字段集都具有相同的名称属性,而我只得到最后一个.如何获取所有行并在我的 submitAction() 控制器中验证它们?每个输入都需要有唯一的名称,对吗?...也许我需要以某种方式设置 name="something[name][]" 但怎么做?

解决方案

好吧,Cerad 的评论是对的,我们必须为此使用集合.乍一听,这可能是胡说八道,但它是对的.我花了一段时间才理解它.

所以我必须创建一个 ProductsType,它是一个 arrayCollection 并插入每个产品.(就像在带有任务和标签的文档中一样)

我用过:

$repository = $this->getDoctrine()->getRepository('ExampleBundle:Product');$products = $repository->findAll();$productCollection = 新产品;foreach ($products as $product) {$productCollection->getProducts()->add($product);}$collection = $this->createForm(new ProductsType, $productCollection);return $this->render('ExampleBundle:Default:index.html.twig', array('集合' =>$collection->createView()));

然后在树枝上:

{% for product in collection.products %}{{ form_row(product.id) }}{{ form_row(product.name) }}{{ form_row(product.description) }}<br clear="all"/>{% 结束为 %}

工作完成.

甚至您可以通过以下方式将主题应用于每一行:

{% block _productsType_products_entry_name_row %}<div class="yourDivName">{{ block('form_widget') }}</div>{% 结束块 %}{% 块 _productsType_products_entry_description_row %}<div class="yourDivDescription">{{ block('form_widget') }}</div>{% 结束块 %}

很酷的东西!

I create a simple form with multiple rows:

Controller:

public function indexAction() 
{
    $repository = $this->getDoctrine()->getRepository('MyBundle:Product');
    $products = $repository->findAll();

    foreach ($products as $product) {
        $forms[] = $this->createForm(new ProductType, $product)->createView();
    }

    return $this->render('MBundle:Default:index.html.twig', array('form' => $forms);        

}

I render this in a twig:

<form action="{{ path('_submit') }}" method="post">
{% for key, formData in forms %}
    {{ form_row(formData.id) }}
    {{ form_row(formData.name) }}
    {{ form_row(formData.nameEnglish) }}
    <br clear="all" />
{% endfor %}
</form>

When i submit form each of my input field set has the same name attributes and i get only the last one. How to grab all the rows and validate them in my submitAction() controller? Each input needs to have unique name, right? ... and perhaps i need to set somehow name="something[name][]" but how to do it?

解决方案

Ok Cerad was right with his comment and we have to use collection for this. It may sound like a nonsense at first but it kinda is right. It took me a while to get head around it.

So i had to create a ProductsType which is an arrayCollection and inserts each Product. (just like in documentation with Task and tags)

I used that:

$repository = $this->getDoctrine()->getRepository('ExampleBundle:Product');
$products = $repository->findAll();

$productCollection = new Products;

foreach ($products as $product) {
    $productCollection->getProducts()->add($product);
}

$collection = $this->createForm(new ProductsType, $productCollection);

return $this->render('ExampleBundle:Default:index.html.twig', array(
    'collection' => $collection->createView()
        ));

Then in twig i do:

<div class="products">
    {% for product in collection.products %}
        {{ form_row(product.id) }}
        {{ form_row(product.name) }}
        {{ form_row(product.description) }}
        <br clear="all" />
    {% endfor %}
</div>    

Job done.

And even you can apply themes to each row by this:

{% block _productsType_products_entry_name_row %}
    <div class="yourDivName">{{ block('form_widget') }}</div>
{% endblock %}
{% block _productsType_products_entry_description_row %}
    <div class="yourDivDescription">{{ block('form_widget') }}</div>
{% endblock %}

Cool stuff!

这篇关于symfony2 中同一实体的表单中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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