jeudi 18 février 2016

In Symfony2, how to set previously 'true' checkboxes to false?

I have a weird issue.

When I submit a Symfony form, the entity property linked to an unchecked checkbox is not updated by the framework to its correct value (false) if the form has previously been sent with the checkbox checked.

Here is how I build my form:

$builder
    ->add('myinteger', 'integer')
    ->add('positionSelection', 'choice', array(
        'choices'  => array('p1'=>'pos1', 'p2'=>'pos2', ...) ,
        'expanded' => true,
        'multiple' => true
    ))
    ->add('secret', 'checkbox', array(
        'required' => false
    ));

And here is an extract of my entity properties:

/** @ORM\Column(name="myinteger", type="integer") */
protected $myinteger;

/** @ORM\Column(name="positionSelection", type="simple_array") */
protected $positionSelection;

/** @ORM\Column(name="secret", type="boolean") */
protected $secret;

I send a first PUT request with the following content (dixit Symfony's profiler):

** REQUEST 1 **
[
    myinteger => 5201, 
    positionSelection => [
        0 => pos1,
        1 => pos4
    ],
    secret => true
]

This one works, and the object is updated in database. However if I now send a new PUT request, which Symfony's profiler sees like this (notice we've checked pos5 instead of pos4 and secret is not checked anymore):

** REQUEST 2 **
[
    myinteger => 5202, 
    positionSelection => [
        0 => pos1,
        1 => pos5
    ]
]

...the object is not correctly updated in database:

  • pos1 keep its 'true'/'checked' value [OK]
  • pos5 is set to 'true'/'checked' value [OK]
  • pos4 keep its 'true'/'checked' value [NOK]
  • secret keep its 'true'/'checked' value [NOK]

NB. The integer myinteger is always correctly updated in the database.

Where should I look to solve this issue?


Additional information (maybe useless):

From what I understand of the Profiler Form section, it looks like each request sets the checked items as default data: for the second request, the 'Default data' for positionSelection strangely was:

DEFAULT DATA:
Normalized Format: [0 => pos1, 1 => pos4]




Aucun commentaire:

Enregistrer un commentaire