28

I am using

$form->input('Model.name', array('multiple'=>'checkbox');

I am trying to base on model data to set certain checkboxes to checked.

How can i do that?

sth
  • 222,467
  • 53
  • 283
  • 367
Dave
  • 281
  • 1
  • 3
  • 3

12 Answers12

31

cmptrgeekken's solution works for a single checkbox. I'm assuming you're generating a multiple checkboxes, for a HABTM relation or something similar.

You need to pass a array with the values of the elements that are going to be selected to the method, like this:

$options = array(1 => 'ONE', 'TWO', 'THREE');
$selected = array(1, 3);
echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' => $options, 'selected' => $selected));

is going to generate this:

 <div class="input select">
      <label for="ModelName">Name</label>
      <input name="data[Model][name]" value="" type="hidden">

      <div class="checkbox">
           <input name="data[Model][name][]" checked="checked" value="1" id="ModelName1" type="checkbox">
           <label for="ModelName1" class="selected">ONE</label>
      </div>
      <div class="checkbox">
           <input name="data[Model][name][]" value="2" id="ModelName2" type="checkbox">
           <label for="ModelName2">TWO</label>
      </div>
      <div class="checkbox">
           <input name="data[Model][name][]" checked="checked" value="3" id="ModelName3" type="checkbox">
           <label for="ModelName3" class="selected">THREE</label>
      </div>
 </div>

The first and third checkbox checked.

Just remember that you're actually working with a multiple select element that is just displayed as a bunch of checkboxes (Which is IMO better because of the usability).

Marko
  • 3,499
  • 3
  • 24
  • 22
  • Speaking of usability, I found that a good trick to use when creating multiple-selects is to put a wrapper-div around the options, put a border on it and have css apply overflow: auto; – spelley Dec 26 '09 at 23:14
  • Took me a while to figure out, but one can have all options checked by doing `echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' => $options, 'selected' => array_keys($options)));` – chmac Oct 05 '12 at 10:38
26

I don't use CakePHP, but according to the docs, it appears as though you should be able to add the option 'checked'=>true:

$form->input('Model.name', array('type'=>'checkbox','checked'=>true));

since that's one of the options of the checkbox function.

cmptrgeekken
  • 8,052
  • 3
  • 29
  • 35
2
echo $this->Form->input('Title', array('type'=>'checkbox', 'label'=>'Label', 'checked'=>'checked'));
Ari
  • 934
  • 1
  • 10
  • 15
2

The Marko solution still working in CakePHP 2.0+

-> https://stackoverflow.com/a/1962499/3197383

It just need to correct with the new syntax :

<?php
$options = array(1 => 'ONE', 'TWO', 'THREE');
$selected = array(1, 3);
echo $this->Form->input('ModelName', 
    array('multiple' => 'checkbox', 'options' => $options, 'selected' => $selected)
);
?>
Community
  • 1
  • 1
Rémi Becheras
  • 14,902
  • 14
  • 51
  • 81
2
$options = array(1 => 'ONE', 'TWO', 'THREE');
$selected = array(1, 3);
echo $form->input('Model.name', 
    array( 
        "name"=>$mnus['Aco']['id'],
        "type"=>"select",
        "multiple"=>"checkbox", 
        'options' => $options, 
        'selected' => $selected)
    );

this is the correct way for multiple check box and checked option. I am using this in cake1.3 please recheck once on your code it must work.

yvoyer
  • 7,476
  • 5
  • 33
  • 37
Gaurav
  • 21
  • 1
2

Its Super Simple

$form->input('field_name', array('type'=>'checkbox','checked'=>true));

That's it.

Documentation: https://book.cakephp.org/3.0/en/views/helpers/form.html

M.suleman Khan
  • 576
  • 6
  • 17
0
$options = array("fixed","varry");
$selected = "0";

echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' => $options, 'value' => $selected));

Use the value attribute to make checked default.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Pankaj Wanjari
  • 1,275
  • 2
  • 9
  • 11
0

Another way to have a checkbox checked with the "label" right next to it is.

$form->checkbox('Model.name', array('checked'=>'checked'))?> Label 

Label can be what ever you want though. example: 21,000-3000, Tire, Human. I am sure you get the idea.

Stirling
  • 4,308
  • 3
  • 23
  • 16
0
'likes_preferences' =>array(
    'type'=>'select','label' => 'Main likes/preferences',
    'options' => $this->Ethos->toOptionsArray('preferences'),
    'multiple' => 'checkbox', 
    'div'=>array('class'=>'input select checkbox-group clearfix'),
    'hiddenField' => false,
),

the above code for adding the data, you need to change the field 'likes_preferences' from array to comma separated string before saving into database.

$preferences = implode(',',$this->request->data['Member']['likes_preferences']);
$this->request->data['Member']['likes_preferences'] = $preferences;

EDIT MODE

$likes = explode(',',$this->request->data['Member']['likes_preferences']);

'likes_preferences' =>array(
    'type'=>'select','label' => 'Main likes/preferences',
    'options' => $this->Ethos->toOptionsArray('preferences'),
    'multiple' => 'checkbox', 
    'div'=>array('class'=>'input select checkbox-group clearfix'),
    'hiddenField' => false,
    'selected' => $likes
),

you are done, again you must convert the array to string while updating the database in edit action.

Dinker
  • 21
  • 5
0
<?php  

$subjects = array(1=>'Snow boarding',2=>'Surfing',3=>'Trekking',4=>'Swimming');
$selected_skills = array(0=>2,1=>4);

// For MutiSelect box with selected 
 $form->input('skills_list',array('label' => 'Skills','options' => $subjects,'class' =>'','multiple'=>true,'selected'=> $selected_skills));

//For Multiple checkbox with checked 
$form->input('skills_list',array('label' => 'Skills','options' => $subjects,'class' =>'','multiple'=>'checkbox','selected'=> $selected_skills));
?>
sth
  • 222,467
  • 53
  • 283
  • 367
0

Here is a small code snippet from one of my project-

    $categories = $this->Site->Category->find('list', array('recursive' => -1));
    $this->set(compact('categories'));

    $this->Site->Category->bindModel(array('hasOne' => array('CategoriesSite')));
    $selected = $this->Site->Category->find('list', array(
        'fields' => array('id'),
        'conditions' => array(
            'CategoriesSite.site_id' => $this->data['Site']['id'],
        ),
        'recursive' => 0,
    ));
    $this->set(compact('selected'));

Main key is for selected is 'fields' => array('id')

Rifat
  • 7,628
  • 4
  • 32
  • 46
0

You can also try this for input with single option

$this->Form->input('name', array('type' => 'checkbox', 'default' => 1, 'checked' => 'checked'));
mouafus
  • 148
  • 7