『CakePHP』を使ってみる 〜7〜 CakePHP 1.2 でプルダウン、チェックボックス、ラジオで作る
CakePHP 1.2 で何か簡単なものを作ろうと思ったけど、その前にプルダウン、チェックボックス、ラジオの作り方を確認。
プルダウン用の配列を取得
モデルにプルダウン用の配列を作ってくれるメソッドが generateList が用意されているそうなので、それを使う。
CAKE/libs/model/model.php を見てみると引数などは以下のようになっていた。
/** * Returns a resultset array with specified fields from database matching given conditions. * Method can be used to generate option lists for SELECT elements. * * @param mixed $conditions SQL conditions as a string or as an array('field' =>'value',...) * @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") * @param integer $limit SQL LIMIT clause, for calculating items per page * @param string $keyPath A string path to the key, i.e. "{n}.Post.id" * @param string $valuePath A string path to the value, i.e. "{n}.Post.title" * @param string $groupPath A string path to a value to group the elements by, i.e. "{n}.Post.category_id" * @return array An associative array of records, where the id is the key, and the display field is the value * @access public */ function generateList($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null, $groupPath = null) {
sampleというアクションの中で呼び出してみる。
function sample() { $list = $this->Post->generateList(null, 'Post.id DESC', null, '{n}.Post.id', '{n}.Post.title'); $this->set('list', $list); }
またもや 1.2 では非推奨になったよ、と警告が。
Model::find("list") か Model::find("all")を使えとのこと。
Warning (512): (Model::generateList) Deprecated, use Model::find("list") or Model::find("all") and Set::combine() [CORE/cake/libs/model/model.php, line 2191]
プルダウンを作る
警告を受けて以下のように直してみた。
function sample() { //$list = $this->Post->generateList(null, 'Post.id DESC', null, '{n}.Post.id', '{n}.Post.title'); $list = $this->Post->find("all"); $list = Set::Combine($list, '{n}.Post.id', '{n}.Post.title'); $this->set('list', $list); }
対応する sample.thtmlは以下のように。
<h4>Pulldown</h4> <?php echo $form->create("Post", array("action" => "sample" ))?> <?php echo $form->input('Post.pulldown', array('type' => 'select', 'options' => $list) );?> <?php echo $form->submit('submit!')?> <?php echo $form->end();?>
プルダウン完成。
ラジオボタンを作る
次はラジオボタン。データは同じものを流用。
ラジオも HTML ヘルパーを使うとエラーになるので FORM ヘルパーの input を使って作る。
『CAKE/libs/view/helpers/form.php』の input() と radio()を見ながら試してみた。
/** * Creates a set of radio widgets. * * @param string $fieldName Name of a field, like this "Modelname.fieldname" * @param array $options Radio button options array. * @param array $attributes Array of HTML attributes. Use the 'separator' key to * define the string in between the radio buttons * @return string */ function radio($fieldName, $options = array(), $attributes = array()) {
input()の呼び出してtypeを指定するとその中で radio()が呼ばれる。
そのまま使ってもOKだけど、inputで作ってきたのでinputから呼ぶ。
<h4>Radio</h4> <?php echo $form->create("Post", array("action" => "sample" ))?> <?php echo $form->input('Post.radiobutton', array( 'type'=> 'radio', 'options' => $list, // データ 'legend' => false, // falseにしないと枠がついてしまう 'div' => false, // 勝手にdiv で囲まれる 'label' => false, // ラベル 'separator' => '<br />',// ラジオボタンのセパレータ 'value' => 2, // とりあえず 2番目を指定 ) );?> <?php echo $form->submit('submit!')?> <?php echo $form->end();?>
ラジオも完成。
スタイルシートの関係か表示がおかしいけど、出力されたソースだけ抜き出して表示されるとちゃんとなったので問題なさそう。
チェックボックスを作る
8/29追記
1.2 での複数チェックボックスの作り方についてトラックバックをいただきました。
echo $form->input('モデル名', array('multiple'=>'checkbox', 'type'=>'checkbox'));
上記のようにすることで、複数選択可能なselectタグが、checkboxに変わります。
なるほど。
検索してみたら、今はマニュアルが充実してきていて以下のページに説明が作られていました。
一度全部読み直した方が良さそうだなあ。
- 7.3.3.5 $options[‘multiple’]
→追記ここまで。↓以下は以前に書いた内容。
複数チェックボックスを作りたいのだけど、うまくいかない。
<h4>Checkbox</h4> <?php echo $form->create("Post", array("action" => "sample" ))?> <?php foreach($list as $k => $v){?> <?php echo $form->input('Post.chbox', array('id' => $k, 'type' => 'checkbox', 'value' => $k, 'label' => $v, 'div' => false, ) );?> <?php } ?> <?php echo $form->submit('submit!')?> <?php echo $form->end();?> <?php } ?>
これで表示的には正しくできているのだけど、出力されるHTMLが変。
<input type="hidden" name="data[Post][chbox]" value="0" id="3_" /> <input type="checkbox" name="data[Post][chbox]" id="3" value="3" /><label for="3">Title strikes back</label> <input type="hidden" name="data[Post][chbox]" value="0" id="2_" /> <input type="checkbox" name="data[Post][chbox]" id="2" value="2" /><label for="2">A title once again!</label> <input type="hidden" name="data[Post][chbox]" value="0" id="1_" /> <input type="checkbox" name="data[Post][chbox]" id="1" value="1" /><label for="1">The title</label>
hidden とかいらないんだけど…。
あとは、データを配列で取れるよう『Post.chbox[]』とnameを指定してみたら、『name="data[Post][chbox[]]"』となってしまいうまく動作しなかった(カッコがちゃんと表示されていないので全角にしてみました)。
ソースを読んでみるとこれは一個のチェックボックスを作るためのものらしく複数ではうまくいかないみたい?
チェックボックスは自分で書かないとダメかなあ。
とりあえず今度は何かを作ってみる。