user198003
user198003

Reputation: 11151

Multiple checkboxes in CakePHP - how to set which are checked?

I have multiple checkboxes in CakePHP's Add/Edit view, created with:

echo $this->Form->input('email_warning_chb', array('type'=>'select', 'multiple'=>'checkbox', 'label'=> __('Email notice'), 'class'=>'multiple-chb', 'options'=> array('title...'=>array( '5'=>'5 days', '15'=>'15 days', '30'=>'30 days', '60'=>'60 days');

My question is how to set which one are checked by default (ie. in thi example, 5, 15 and 60)?

Thank you in advance!

Upvotes: 8

Views: 36378

Answers (3)

Pent
Pent

Reputation: 1049

As said in other answers, you should set the 'selected' option. What some people don't mention is that your selected array should only contain the id in each element. Example:

$selectedWarnings = $this->Warning->find('list', array(
  'fields' => array('id')
));


echo $this->Form->input('email_warning_chb', array(
    'label' => 'Email Notice',
    'type' => 'select',
    'multiple' => 'checkbox',
    'options' => $warnings,
    'selected' => $selectedWarnings
  ));

Upvotes: 12

Krishna
Krishna

Reputation: 1540

in your controller you have to put the value like this:

$this->request->data['Model']['email_warning_chb'] = array(5,15,60);

and it will automatically display checkbox as selected.

Please ask if not work for you.

Upvotes: 1

user1593705
user1593705

Reputation:

this looks like this one

cakephp: How to set checkbox to checked?

where $selected contains the selected values

Upvotes: 3

Related Questions