Home > CakePHP > CakePHP 1.2系でFormHelperを使う(Radio編)

CakePHP 1.2系でFormHelperを使う(Radio編)

CakePHP1.2系で追加されたヘルパーのFormHelper(フォームヘルパー)を使った話です。

どうしてもラジオボタンが言う事を聞いてくれないんですよね・・・。
勝手に括弧が付いたり、フィールド名で括られたり。
っていうことで、解析。

ヘルパーのプログラムファイルを覗いてみましょう。

インストールディレクトリ/cake/libs/view/helpers/form.php

このフォルダ内に、全てのヘルパーファイルがあるんで困ったらここを見るといいですよ。
ここでFormヘルパーの部分をチェック。

742行目あたりがそうですね。一部を抜粋。


/**
* 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()) {
$attributes = $this->__initInputField($fieldName, $attributes);
$this->__secure();

$legend = false;
if (isset($attributes['legend'])) {
$legend = $attributes['legend'];
unset($attributes['legend']);
} elseif (count($options) > 1) {
$legend = Inflector::humanize($this->field());
} $label = true; if (isset($attributes['label'])) { $label = $attributes['label'];
unset($attributes['label']); } $inbetween = null;
if (isset($attributes['separator'])) { $inbetween = $attributes['separator'];
unset($attributes['separator']);
}

if (isset($attributes['value'])) {
$value = $attributes['value'];
} else {
$value = $this->value($fieldName);
}

$out = array();

ここの所のFunctionの引数に注目。

function radio($fieldName, $options = array(), $attributes = array())

ふむふむ、ってことは、radioは第3引数までで、第2と第3はArrayで渡せと。

で、該当する中身を読む。
$attributesの配列の中に入れられるものは3つの変数。$legendと$labelと$separator。
この3つのどれかでなるだろ・・・と思っていじったら出来た。

もし、枠囲いされてフィールド名が出て邪魔だと思う場合は・・・

< ?php echo $form->radio(’フィールド名’,array(’選択候補1’ => ‘POST値’ , ‘選択候補2′ => ‘POST値’),array(’legend’ => false)); ?>

ポイントはlegendという値ですね。

legend = false →枠が表示されない(フィールド名が表示されない)
legend = true →「1」と表示
legend = ‘文字列’ →枠囲いの表題が「文字列」に。

何はともあれ、悩んだ末に出来てよかった・・・。

というかCakePHPの1.2に関する記述がやたら少なくて、見つけ出すのも一苦労ですねぇ・・・。

メモ書き程度にここに色々残していきますが、動かないとか出来ない報告あれば対応します。

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://manjiro.net/archives/72/trackback
Listed below are links to weblogs that reference
CakePHP 1.2系でFormHelperを使う(Radio編) from デジタル料理人 - Webプログラマコンサル屋 -

Home > CakePHP > CakePHP 1.2系でFormHelperを使う(Radio編)

カテゴリー
blog chart

Return to page top