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

后端 未结 3 1181
爱一瞬间的悲伤
爱一瞬间的悲伤 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:31

    this looks like this one

    cakephp: How to set checkbox to checked?

    where $selected contains the selected values

    0 讨论(0)
  • 2021-01-05 01:39

    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.

    0 讨论(0)
  • 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
      ));
    
    0 讨论(0)
提交回复
热议问题