Setting checkboxes as checked in model

edited February 2016 in Modules
I'm using a multi type field inside form_fields() to set bit values as checkboxes.

I've then used on_before_save() to accumulate the bits and sum them into another field.

Now I'm puzzling how to re-set the checked state in the edit view?

My current solution: I've set a javascript array in a js key, with the bit values bit-wised out again, and re-check the checkboxes using that source from controller.js. This is OK but feels like cheating. Is there a way of setting the options in the multi as checked? With a form_builder like set_values()?

To be clear I have this in the model:
public $ages = array( 1 => '4', 2 => '5', 4 => '6', 8 => '7', 16 => '8', 32 => '9', 64 => '10', 128 => '11', 256 => '12', 512 => '13', 1024 => '14', 2048 => '15', 4096 => '16' ); .... $fields['ages'] = array( 'type' => 'multi', 'options' => $this->ages, 'mode' => 'checkbox', 'row_class' => 'age-range' );

Comments

  • edited 11:53AM
    You can set the "value" parameter to be the array of selected checkbox values (e.g. 1, 2, 4, 8, 16...etc). This may mean that in the form_fields method you need to create that array since by pulling in the values from that field since it won't be in the edit_method by default (which by default is the find_one_array method on the model).
Sign In or Register to comment.