dimanche 20 septembre 2015

Field becomes NULL when submitting the form, except for the first register in the table

I have two related tables: material and items_budget. The items_budget form lists all the materials as a group of checkbox, and beside each checkbox, I put two input fields, one for quantity and one for price. Below is the code for listing these Material:

<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 %}

I also have a trigger that is executed whenever new data is persisted into items_budget. It subtracts the entered quantity from the currentQuantity of material table. It works perfectly, but now comes the problem: it only works for the first register in the material table, for the other ones, the quantity field mysteriously becomes NULL after submitting the form.

I tried to add at the end of each name attribute a pair of brackets, but by doing it, the form is simply refreshed and no data is persisted at all.

I really want to make it as clear as possible, so I took four pictures of the steps I did. From top-left to bottom-right: I first marked the first register in my materials table, entered a random quantity and persisted it. As you can see in the first image of my table, it's normal. But now, the other two images show the problem. I chose another register and entered another value for quantity, but this time, quantity is NULL.

enter image description here

The ItemsBudgetController and the addAction:

public function addAction(Request $request)
{
    $form = $this->createForm(new ItemsBudgetType());
    $manager = $this->getDoctrine()->getManager();

    if ($request->getMethod() == 'POST') {
        $form->handleRequest($request);

        if ($form->isValid()) {
            $ItemsBudgetEntity = $form->getData();
            $manager->persist($ItemsBudgetEntity);
            $manager->flush();

            $BudgetEntity = $form->get('budget')->getData();
            $BudgetEntity->addItemsBudget($ItemsBudgetEntity);

            $manager->persist($BudgetEntity);
            $manager->flush();

            $this->addFlash('success', 'Materiais para o orçamento adicionados');

            return $this->redirect($this->generateUrl('panel_budgets'));
        }
    }

    return $this->render('PanelBundle:ItemsBudget:index.html.twig', array(
        'budgets' => $manager->getRepository('PanelBundle:Budget')->findAll(),
        'materials' => $manager->getRepository('PanelBundle:Material')->findAll()
    ));
}

And now , the ItemsBudgetType, where I configure the material field as an entity:

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',
                ),
            ));
}

It doesn't make any sense. Why the first register only has its quantity "seen"? If you need any more code, comment and I will update the question.




Aucun commentaire:

Enregistrer un commentaire