複数checkbox 1フィールドで配列で取得

[php]
//コントローラー
public function add() {
   if ($this->request->is(‘post’)) {

if($this->request->data[‘Lecture’][‘name’] == false){
       //対象データがなかった処理
array(‘Lecture’ => array(
‘name’ => ‘test’
));
}
if ($this->request->data[‘Lecture’][‘name’] == true) {
//対象データがあった処理
$this->request->data[‘Lecture’][‘name’] = implode( ‘,’, $this->data[‘Lecture’][‘name’]); //カンマで区切るよ
}
     
     $this->Lecture->create();
   if ($this->Lecture->save($this->request->data)) {
$this->Session->setFlash(__(‘The lecture has been saved.’));
 return $this->redirect(array(‘action’ => ‘index’));
   } else {
$this->Session->setFlash(__(‘The lecture could not be saved. Please, try again.’));
}
    }
$numbers = $this->Lecture->Number->find(‘list’);
$this->set(compact(‘numbers’));
}
[/php]
[php]
//view
$select1 = array(‘test’,’test1′);
echo $this->Form->input( ‘name’, array(
‘type’ => ‘select’,
‘multiple’=> ‘checkbox’,
‘options’ => $select1,
//’selected’ => $selected // 規定値は、valueを配列にしたもの
));
[/php]
[php]
//editコントローラー
$test= $this->Lecture->find(‘first’, array(
‘conditions’ => array(‘Lecture.id’ => $id),
‘fields’ => array(‘Lecture.name’), //フィールド名の配列
));
$this->set( ‘selected’, explode( ‘,’, $this->$test[‘Lecture’][‘name’]));
[/php]
[php]
//view editコントローラー
$select1 = array(‘test’,’test1′);
echo $this->Form->input( ‘name’, array(
‘type’ => ‘select’,
‘multiple’=> ‘checkbox’,
‘options’ => $select1,
‘selected’ => $selected // 規定値は、valueを配列にしたもの ここね
));
[/php]

参考:http://kwski.net/cakephp-2-x/1088/

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です