Note that there are some explanatory texts on larger screens.

plurals
  1. POsymfony setting value of specific checkbox widgets in form
    primarykey
    data
    text
    <p>I have a widget defined with a series of checkboxes that is essentially used to build up a bit flag. So I have a DB column 'access_bits', and I have several checkboxes generated on in the form class, in the format of 'access_bits[]', with the appropriate values (i.e. 1,2,16,1024 etc)</p> <p>following the solution on this post (<a href="https://stackoverflow.com/questions/2254620/symfony-how-to-change-a-form-field-attribute-in-an-action">Symfony: How to change a form field attribute in an action?</a>) - which is simple and sensible - I am able to magically set the attribute for <strong>all</strong> the checkboxes. But how can I target specific ones (preferably by value)? </p> <p><strong>TO CLARIFY:</strong></p> <p>HTML:</p> <pre><code>&lt;input name="user[access_bit][]" value="1" /&gt; Level 1 &lt;input name="user[access_bit][]" value="2" /&gt; Level 2 &lt;input name="user[access_bit][]" value="4" /&gt; Level 3 &lt;input name="user[access_bit][]" value="8" /&gt; Level 4 </code></pre> <p>In the DB I have a column that has a bitwise integer. So for example, to have Level 1 and Level 2 checked, the value in the column would be 3. To have them all checked, the value would be 15.</p> <p>"What I Want" PHP in action.class.php</p> <pre><code>$exampleValue = 4; foreach ($form-&gt;getWidget('access_bit') as $thisWidget) { if ($thisWidget-&gt;getValue() == $exampleValue) { $thisWidget-&gt;setAttribute('checked','checked'); } } </code></pre> <p>... which would cunningly set the checkbox next to Level 3 as ticked.</p> <p>I do not want to use jQuery to do this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload