超基本的な事ですが、CakePHP2.x のFormヘルパーでラジオボタンを作る方法。
アンチョコ化してコピペで作れるよにしておいてやる。
CakePHP2でラジオボタンを作る
文法
第一引数がオプション(選択肢)、第二引数がアトリビュート(設定)。
legendをtrueにすると囲まれます。
英語版のCookbook 2.xから転載。訳は超適当。
$attributes[‘value’]
to set which value should be selected default.
→ 規定値を指定
$attributes[‘separator’]
to specify HTML in between radio buttons (e.g. <br />).
→ HTMLでセパレータを指定(<br />とか)
$attributes[‘between’]
specify some content to be inserted between the legend and first element.
→ Legendと最初の値の間に表示するテキストを指定(ラベルみたいなもの??)
$attributes[‘disabled’]
Setting this to true or ‘disabled’ will disable all of the generated radio buttons.
→ “disabled”=>trueか”disabled”=>”disabled”で、フォーム自体がdisabledになります。
$attributes[‘legend’]
Radio elements are wrapped with a label and fieldset by default. Set $attributes[‘legend’] to false to remove them.
→ デフォルトで囲まれるので、legendにfalseをセットすることで囲まれなくなるよ。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14  | 
						echo $this->Form->radio(     'gender',      array(         'male'=>'男性',          'female'=>'女性'     ),      array(         'value'=>'none',          'separator'=>" ",          'between'=>'性別 ',          'disabled'=>false,          'legend'=>false     ) );  | 
					
実際の書き方
トピックス(Topic)の「公開 / 非公開」設定で使用する。
$mt_publish は app_model などで以下のように指定し、TopicsController で呼び出して$this->setする。
初期値を1としたい場合は、
$this->request->data[‘Topic’][‘publish’] = 1;
とする。
app/Models/AppModel.php
| 
					 1 2 3 4 5 6 7 8  | 
						/**  * 公開 / 非公開  *   */ var $mt_publish = array(     "0" => "非公開",     "1" => "公開" );  | 
					
app/Controllers/TopicsController.php
| 
					 1 2 3 4 5 6 7 8  | 
						// 公開設定マスタ $mt_publish = $this->Topic->mt_publish; $this->set('mt_publish',$mt_publish); // 公開設定初期値 if ( !isset($this->request->data['TopicType']['publish']) ) {     $this->request->data['Topic']['publish'] = 1; }  | 
					
app/Views/Topics/form.ctp
| 
					 1 2  | 
						echo '公開設定 : '; echo $this->Form->radio('Topic.publish', $mt_publish, array('legend'=>false));  | 
					
CakePHPデフォルトのCSSが効いいるのでラジオボタンが改行していますが、こんな感じになるはずです。
参考ページ
[tgAmazonItemLookup asin=”B00A0CY4QE” related=”1″]

        
        
        
        
        
        
        
        
        
        
        
        
        
        
  
  
  
  
