mardi 22 septembre 2015

How to render a checkbox group with fields from an Entity with Symfony2

I have a material table that is related to items_budget. The form for items_budget needs to list all the registered material as a checkbox group, and beside each checkbox, two input fields, for quantity and price. Below is the code block I wrote for rendering this piece:

<strong>Materiais</strong>
{% for material in materials %}
    <div class="checkbox">
        <label>
            <input type="checkbox" class="itemsbudget_material" name="cdg_itemsbudget_type[material]" value="{{ material.id }}"> {{ material.name }} - 
        </label>
        <input type="hidden" class="itemsbudget_price_hidden" value="{{ material.price }}"/>
        <input type="text" class="itemsbudget_quantity" name="cdg_itemsbudget_type[quantity]" placeholder="Qtd" size="3"/>x - R$
        <input type="text" class="itemsbudget_price" name="cdg_itemsbudget_type[price]" value="0" size="3" readonly/>
    </div>
{% endfor %}

The problem I am facing can be seen in the image. Looking at the top-left image, you see I have marked "Material C", filled the quantity, and after submitting the form, as shown in the second image, everything is fine.

But if I do the same with another Material, as it can be seen in the last image of my table, the quantity field is NULL.

What happens here is that only the first register in the material form is, let's say, "seen". When I say "first register" I mean the lowest id in the material form, literally the FIRST. If I had deleted the "Material C", then "Material D" would be the first and the quantity field would be seen for it only.

enter image description here

I have tried to put at the end of each name attribute a pair of brackets, but by doing it the page is refreshed and nothing is persisted, nothing happens.

I have tried many different ways to render this. Also, I have tried to create my material field as a collection, but I really think this is not the solution because I must get the id, quantity and price entered in the fields. I would need to be able to render something exactly like this with a collection form.

Please I need guidance. If you need any more code I will be happy to update my question. Thank you.




Aucun commentaire:

Enregistrer un commentaire