「link」カテゴリーアーカイブ

CakePHPを用いて複数のチェックボックスの値をサーバサイドで取得する:東京ウェブ制作ブロマガ:東京ウェブ制作チャンネル(大和賢一郎) – ニコニコチャンネル:生活

‘value’ => array(‘0’, 3)

引用元: CakePHPを用いて複数のチェックボックスの値をサーバサイドで取得する:東京ウェブ制作ブロマガ:東京ウェブ制作チャンネル(大和賢一郎) – ニコニコチャンネル:生活.

複数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/

(CakePHP2.x)独自バリデーションルールの書き方 | 日々の覚書…日常のことも少しだけ

//独自バリデーションルール

public function checkOnly($check){

  //チェックしたいルールを書く

  if (!empty($this->data[‘Brunch’][‘id’])){

      //存在するか確認

      $count = $this->find(‘count’, array(‘conditions’ => array(

                               ‘brunch_name’ => $check[‘brunch_name’],

                               ‘company_id’ => $this->data[‘Brunch’][‘company_id’],

                               ‘NOT’ => array(‘Brunch.id’ => $this->data[‘Brunch’][‘id’])

      )));

      //1件でもあればfalseを返す

      return $count == 0;

  }else{

      //新規の場合

      //存在するか確認

      $count = $this->find(‘count’, array(‘conditions’ => array(

   

引用元: (CakePHP2.x)独自バリデーションルールの書き方 | 日々の覚書…日常のことも少しだけ.