不,你不能。从yii\helper\BaseHtml
类代码可以看出,这种嵌套标签来自radio()
方法的源代码,无需通过更改选项进行配置。
你需要的是覆盖该方法。这很容易。
- 在命名空间
app\helpers
中,创建类Html
。并将其放入名为/Helpers/Html.php
相对于您的应用程序根一个新文件(如果你已经有了基本的Yii应用程序),并把这样的事情里面:
namespace app\helpers;
use Yii;
use yii\helpers\BaseHtml;
class Html extends BaseHtml
{
public static function radio($name, $checked = false, $options = [])
{
$options['checked'] = (bool) $checked;
$value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) {
// add a hidden field so that if the radio button is not selected, it still submits a value
$hidden = static::hiddenInput($name, $options['uncheck']);
unset($options['uncheck']);
} else {
$hidden = '';
}
if (isset($options['label'])) {
$label = $options['label'];
$labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
unset($options['label'], $options['labelOptions']);
$content = static::input('radio', $name, $value, $options);
$content .= static::label($label, null, $labelOptions);
return $hidden . $content;
} else {
return $hidden . static::input('radio', $name, $value, $options);
}
}
}
说明:
我们刚刚从yii\helpers\BaseHtml
复制了radio()
方法的代码,并将包含static::label()
的行改为将static::input()
的输出与它分开;
由于原创和我们的自定义类扩展yii\helpers\BaseHtml
,并且原始yii\helpers\Html
未重新定义任何BaseHtml方法,所以除了无线电以外的元素的逻辑将不会丢失。
命名空间和类名称不应该完全相同,但显然它们应该与yii\helpers\Html
不同。
- 只是在你查看的代码替换
use yii\helpers\Html;
与use app\helpers\Html;
。
就这样!