不,你不能。從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;
。
就這樣!