0

When adding a record, I let user mark as many checkboxes and needed:

echo $this->Form->input('us_roles', array('label' => 'Roles:',  'type'=> 'select', 'multiple'=>'checkbox','options' => $arr_pr_role));

and store selected indexes to a string field on my bd without a problem. (it saves eg. 1,2,3)

However, when editing that record, the checkboxes are not populated -selected- accodingly. (based on the string text, for example, 1,2,3

How can I have my checkboxex reflect the values stored as a string on the db?

My edit view uses the same as my add view:

echo $this->Form->input('us_roles', array('label' => 'Roles:',  'type'=> 'select', 'multiple'=>'checkbox','options' => $arr_pr_role));

** More detail

When adding a new record, I implode the selections from the select into my data:

$this->request->data['User']['us_roles'] = implode(",", $this->request->data['User']['us_roles']);

Same thing when saving an edited record.

The issue isUpon retrieval, how can I translate the string into my us_roles input?

echo $this->Form->input('us_roles', array('label' => 'Roles:',  'type'=> 'select', 'multiple'=>'checkbox','options' => $arr_pr_role));

Can you help?

--- update, fixed---

    public function edit($id = null) {
    $this->User->id = $id;
    if (!$this->User->exists()) {
        throw new NotFoundException(__('Invalid user'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
        $this->request->data['User']['us_roles'] = implode(",", $this->request->data['User']['us_roles']);
        if ($this->User->save($this->request->data)) {
...
 } else {
             $this->request->data = $this->User->read(null, $id);
        $this->request->data['User']['us_roles'] = explode(",", $this->request->data['User']['us_roles']);

 }  
Carlos Garcia
  • 359
  • 1
  • 5
  • 29
  • how do you read the data and pass it down to your edit form? how do you populate the `$this->request->data` array so to speak? – mark Jan 08 '13 at 00:31
  • thx @mark, Please see bottom of updated question. – Carlos Garcia Jan 08 '13 at 02:17
  • 1
    Why are you imploding? That is never a good idea for select form fields. – mark Jan 08 '13 at 10:03
  • Thank you @mark. I implode the select values to get a string that can be saved on my string field. It works as is and the string is properly stored. My issue is to populate the select when retrieving that string from db. How can I go to get my checkboxes marked based on the retrieved string? – Carlos Garcia Jan 08 '13 at 20:39

1 Answers1

0

Now I understand... You are looking for the opposite of implode(). That would be explode().

Or you can use the string class ( http://book.cakephp.org/2.0/en/core-utility-libraries/string.html#String::tokenize ):

$array = String::tokenize($string, ',');

should get you your array of values back.

But believe me, there are usually better ways of doing that. You could make it another db table + model. You could use the ArrayDatasource, you could use some bitmasked solution like I do in such a case ( http://www.dereuromark.de/2012/02/26/bitmasked-using-bitmasks-in-cakephp/ ). The advantage of the last approach is that you still use only a single field (and very small amount of space) while you have the full capabilities of an extra table: Full search on NOT, AND, OR, ...

mark
  • 21,691
  • 3
  • 49
  • 71
  • Thank you @mark. Do I have to manually walk through the array to populate my input (multiple checkbox) or is there a way to assign the array (from exploding my db string) to my input? This is to be done in my view right? I really appreciate your help. – Carlos Garcia Jan 08 '13 at 23:35
  • post your data and debug the post data. this is how your array has to look like again in the controller for edit. also, this should be done in the controller populating `$this->request->data` again as with all edit actions. – mark Jan 08 '13 at 23:42
  • see default value chapter here: http://www.dereuromark.de/2010/06/23/working-with-forms/ – mark Jan 08 '13 at 23:51
  • Thank you @mark. I kept it simple -it's a very small list of checkboxes. This worked this time: Upon saving, we implode to save a string on db; then whene editing, we explode to populate the checkboxes. Updated question with sample code to help some beginner like me out there. Best regards. – Carlos Garcia Jan 10 '13 at 03:28