1
My system has two related tables called material and items_budget. The second table has a form that lists the name of each material in a checkbox, followed by two more input
, one for your quantity and another for the price. Here is the code snippet for such:
{% for material in materials %}
<div class="checkbox">
<label>
<input class="itemsbudget_material" type="checkbox" name="cdg_itemsbudget_type[material][]" value="{{ material.id }}"> {{ material.name }} -
</label>
<input class="itemsbudget_quantity" type="text" name="cdg_itemsbudget_type[quantity][]" placeholder="Qtd" size="5"/>x - R$
<input class="itemsbudget_price_hidden" type="hidden" value="{{ material.price }}"/>
<input class="itemsbudget_price" type="text" name="cdg_itemsbudget_type[price][]" value="0" size="5" readonly/>
</div>
{% endfor %}
There is a Trigger that is fired when a new die is inserted in items_budget, whose function is to subtract from the current quantity of the material selected the quantity entered in the form field.
Below the form of items_budget:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('budget', 'entity', array(
'class' => 'PanelBundle:Budget',
'attr' => array(
'class' => 'form-control',
),
))
->add('material', 'entity', array(
'class' => 'PanelBundle:Material',
'attr' => array(
'required' => true,
),
))
->add('quantity', 'number', array(
'attr' => array(
'class' => 'form-control',
),
))
->add('price', 'money', array(
'attr' => array(
'class' => 'form-control',
),
));
}
The problem is that Trigger’s function only works correctly with the first table record material, with the rest, the quantity field is always NULL. According to the code above, I tried to continue placing brackets at the end of the attribute name
, but making this template the form page is only reloaded and no data is inserted in items_budget. Is there any other way to list entities in Symfony2? Thank you!
I think Collection Type is what you need, since you want to associate the same object several times.
– felipsmartins