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

后端 未结 3 1194
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 00:57

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

echo $this->Form->input(\'email_warning_chb\', array(\'type\'=>\'select\', \'m         


        
3条回答
  •  忘掉有多难
    2021-01-05 01:44

    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
      ));
    

提交回复
热议问题