表单控件 control
control 根据配置选择要渲染的部件,适用于字段类型由表单配置决定的页面。直接在视图中声明固定类型的控件时,也可以使用对应的部件函数。
指定控件
control 属性指定控件名。省略时会先读取 type,两者都省略时,有 items 就选择 picker,其余情况使用 input。同时传入两个属性时,type 是具体控件的类型。
php
control(set::control('input'), set::type('email'), set::name('email'));
control(set::name('module'), set::items(['web' => '网站', 'app' => '移动端']));
control(set::control('static'), set::value('待处理'));| 常用控件名 | 生成的内容 |
|---|---|
static、html | 静态文本或 HTML 内容 |
text、hidden、number、password 等 | 对应类型的输入框 |
inputControl、inputGroup | 带前后缀的输入框或组合输入框 |
checkbox | 单个复选框,提供 items 时生成列表 |
checkList、checkListInline | 纵向或水平的复选框列表 |
radioList、radioListInline | 纵向或水平的单选框列表 |
date、time、datetimePicker | 日期、时间、日期时间选择器 |
pri、severity、color | 优先级、严重程度、颜色选择器 |
file、files | 文件选择器 fileSelector |
select、picker、textarea、editor | 同名的 Zin 部件 |
复选与单选
php
control
(
set::control('checkListInline'),
set::name('interest[]'),
set::value(array('code', 'music')),
set::items(array('code' => '编程', 'music' => '音乐', 'read' => '阅读'))
);
control
(
set::control('radioList'),
set::name('contact'),
set::value('email'),
set::items(array('email' => '电子邮件', 'phone' => '电话'))
);自定义渲染
builder 是 PHP 回调,参数依次为移除 builder 后的属性数组和子节点数组。回调的返回值直接作为构建结果。
php
control
(
set::name('estimate'),
set::value('8'),
set::builder(static function(array $props, array $children)
{
return inputControl(set::suffix('小时'), input(set($props), set::type('number')), $children);
})
);属性
| 名称 | 类型 | 可选 | 声明默认值 | 说明 |
|---|---|---|---|---|
control | string | 是 | — | 要渲染的控件名称。 |
type | string | 是 | — | 具体控件的类型。省略 control 时兼作控件名。 |
id | string | 是 | — | 省略时取 name 并去掉末尾的 []。 |
name | string | 否 | — | 提交字段名。static 可省略。 |
value | string | 是 | — | 控件值。 |
placeholder | string | 是 | — | 占位文本。 |
text | string | 是 | — | 文本。 |
readonly | bool | 是 | — | 是否只读。 |
required | bool | 是 | — | 是否为必填。 |
disabled | bool | 是 | — | 是否禁用。 |
builder | callable | 是 | — | 自定义构建函数。 |
items | array | 是 | — | 选项列表。 |