Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

php - multiple rows in form for the same entity in symfony2

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...