使用CakePHP';s FormHelper::radio()方法
我已经彻底地研究了前面的问题,我很惊讶还没有人问这个问题,因为这似乎是一件非常简单的事情 如何使用CakePHP的FormHelper使标签包装输入和标签文本?我使用的是CakePHP2.3.1。使用一些标准选项调用使用CakePHP';s FormHelper::radio()方法,cakephp,Cakephp,我已经彻底地研究了前面的问题,我很惊讶还没有人问这个问题,因为这似乎是一件非常简单的事情 如何使用CakePHP的FormHelper使标签包装输入和标签文本?我使用的是CakePHP2.3.1。使用一些标准选项调用$this->Form->radio(),可以得到: <input id="input_id" type="radio" ... /> <label for="input_id">label text</label> 标签文本 我要找的是 &
$this->Form->radio()
,可以得到:
<input id="input_id" type="radio" ... />
<label for="input_id">label text</label>
标签文本
我要找的是
<label for="input_id"><input type="radio" id="input_id" ... />label text</label>
标签文本
我通过以下方式实现了这一目标:
$this->Form->input('input1',array('options'=>$options,'type'=>'radio',
'label'=>false
'在'=>''之前,
'分隔符'=>'',
'在'=>''之后'
));
但显然,这个解决方案并不理想。有人能告诉我CakePHP是否有一种更简单和“更合适”的方法来实现这一点吗?扩展助手,并创建自己的方法
<?php
// app/views/helpers/radio.php
class RadioHelper extends AppHelper {
function display($id, $options = array()) {
if (isset($options['options']) && !empty($options['options'])) {
$rc = "";
foreach ($options['options'] as $option) {
$rc .= "<label>";
$rc .= "<input ....>";
$rc .= "</label>";
}
return($rc);
}
return(false); // No options supplied.
}
}
?>
只需确保将表单帮助程序中的逻辑复制到您自己的帮助程序中
另外,如果你只是添加了一个方法,你不太可能需要一个完整的助手。只需将函数“display”添加到app_helper.php中,并从您已加载的任何“其他”帮助程序中引用它,因为它们扩展了app_helper,您将在所有儿童帮助程序中使用display方法。我在试图自己找到答案并解决它时遇到了这个问题。您不需要更改/扩展类;它可以通过传递适当的选项来实现 这就是我所做的,它与引导一起工作:
$options = array(
'1' => 'One',
'2' => 'Two'
);
$attributes = array(
'class' => '',
'label' => false,
'type' => 'radio',
'default'=> 0,
'legend' => false,
'before' => '<div class="radio"><label>',
'after' => '</label></div>',
'separator' => '</label></div><div class="radio"><label>',
'options' => $options
);
echo $this->Form->input('myRadios', $attributes);
$options=array(
“1”=>“一”,
“2”=>“2”
);
$attributes=数组(
“类”=>“”,
'label'=>false,
'类型'=>'无线电',
“默认值”=>0,
“图例”=>错误,
'在'=>''之前,
'在'=>''之后,
'分隔符'=>'',
“选项”=>$options
);
echo$this->Form->input('myRadios',$attributes);
这将使每个收音机都有自己的
,以符合引导标记。如果您只想使用简单的标签包装,请将div
元素从before
、before
和分隔符中删除,我刚刚注意到您说您正在使用cake 2.3.1,这与cake 2.3.1略有不同,但概念仍然相同。
<?php
// some_controller.php
var $helpers = array('Radio');
?>
<?php
// some_view.ctp
echo $this->Radio->display('input1', array('options' => $options));
?>
$options = array(
'1' => 'One',
'2' => 'Two'
);
$attributes = array(
'class' => '',
'label' => false,
'type' => 'radio',
'default'=> 0,
'legend' => false,
'before' => '<div class="radio"><label>',
'after' => '</label></div>',
'separator' => '</label></div><div class="radio"><label>',
'options' => $options
);
echo $this->Form->input('myRadios', $attributes);