radio 字段渲染一系列单选选项,其中一次只能选择一个项目。单选字段支持与 下拉字段类型 相同的选项定义方法。
security_level:
type: radio
label: Access Level
options:
all: All
registered: Registered only
guests: Guests only以下 字段属性 常用。
| Property | Description |
|---|---|
| label | a name when displaying the form field to the user. |
| default | a default value to use for new records. |
| options | available options for the radio list, as an array. |
| optionsMethod | take options from a method defined on the model or as a static method, eg Class::method. |
| cssClass | used for setting the options as inline. |
| inlineOptions | display the options side-by-side instead of stacked. |
您可以使用 default 属性来设置一个默认值,其中该值是选项的键。
security_level:
type: radio
label: Access Level
default: guests除了一个简单数组之外,单选列表支持一个次要描述,作为其options的一部分。
security_level:
type: radio
label: Access Level
options:
all: [All, Guests and customers will be able to access this page.]
registered: [Registered only, Only logged in member will be able to access this page.]
guests: [Guests only, Only guest users will be able to access this page.]为了直观地并排显示选项而不是堆叠显示,请将 inlineOptions 属性设置为 true 值。
security_level:
type: radio
label: Access Level
inlineOptions: true单选列表支持与下拉字段类型相同的选项定义方法。
除了这些定义之外,对于单选列表,该方法可以返回一个简单数组:key => value 或一个数组的数组,用于提供描述:key => [label, description]。
public function listAccessLevels($fieldName, $value, $formData)
{
return [
'all' => ['All', 'Guests and customers will be able to access this page.'],
// ...
];
}