# 数据表单
# 快速入门
ModStart\Form\Form
类用于快速生成表单页面,参照例子 数据表格→快速入门
可以通过如下代码快速定义个数据表单
return Form::make('blog', function (Form $form) {
// 定义标题字段,格式为单行文本
$form->text('title', '标题');
// 定义封面字段,格式为单张图片
$form->image('cover', '封面');
// 定义摘要字段,格式为多行文本
$form->textarea('summary', '摘要');
// 定义内容字段,格式为富文本
$form->richHtml('content', '内容');
});
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 获取当前模型数据
在闭包内可以获取到当前模型的数据
return Form::make('blog', function (Form $form) {
dd($form->item());
});
1
2
3
2
3
# 数据保存阶段自定义处理
# 保存前
$form->hookSaving(function (Form $form) {
if ($form->isModeAdd()) {
// 增加模式
$data = $form->dataAdding();
$data['xxx'] = 'xxx';
// 替换入库数据
$form->dataAdding($data);
} else if ($form->isModeEdit()) {
// 修改
$data = $form->dataEditing();
$data['xxx'] = 'xxx';
// 替换入库数据
$form->dataEditing($data);
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 保存后
$form->hookSaved(function(Form $form) {
// 保存后的操作
});
1
2
3
2
3
# 数据变更
$form->hookChanged(function (Form $form) {
// 可以在数据变更时自定义处理,一般用于清缓存等操作
});
1
2
3
2
3
# 删除前
$form->hookDeleting(function (Form $form) {
// 数据处理
});
1
2
3
2
3
# 删除后
$form->hookDeleted(function (Form $form) {
// 数据处理
});
1
2
3
2
3
# 字段组件基础
# 字段定义
$field = $form->text('xxx','字段名称');
1
# 字段自定义渲染
$field->hookRendering(function (AbstractField $field, $item, $index) {
// 是否为普通表单模式、增加表单模式、修改表单模式,可根据不同模式反馈不同的渲染内容
// $field->context()->isModeForm()
// $field->context()->isModeAdd()
// $field->context()->isModeEdit()
return <<<HTML
<div class="line">
<div class="label">内容</div>
<div class="field">
<div style="border:2px solid red;padding:10px;">
字段自定义显示,其中name需要和字段column相同
<input type="text" name="content" value="内容" />
</div>
</div>
</div>
HTML;
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 字段默认值
$field->defaultValue('默认值')
1
# 提示文字
$field->placeholder('提示文字')
1
# 只读模式
$field->readonly(true)
1
# 帮助文字
$field->help('帮助文字,支持HTML')
1
# 字段校验
// 字段必填
$field->required();
// 匹配URL
$field->ruleUrl();
// 在表中必须唯一
$field->ruleUnique('数据表')
// 匹配正则,目前正则中暂不能包含|
$field->ruleRegex('/^\d+$/');
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 字段组件库
# 显示 display
$form->display('field','名称');
1
# 单行文本 text
$form->text('field', '名称');
1
# 多选 checkbox
$form->checkbox('field', '名称');
1
# 标签 tags
$form->tags('field', '名称');
1
# 代码 code
$form->code('field', '名称');
1
# 树状组件 tree
$form->tree('field', '名称');
1
# 类型 type
// 基础用法
$form->type('field', '字段名');
// 定义BaseType,同时指定类型颜色
$field->type(XxxStatus::class)
1
2
3
4
5
2
3
4
5
# 密码 password
$form->password('field', '字段名');
1
# 单张图片 image
$form->image('field', '字段名');
// 使用用户图库
$form->image('field', '字段名')
->server(modstart_web_url('member_data/file_manager/image'));
1
2
3
4
2
3
4
# 多张图片 images
$form->images('field', '字段名');
// 使用用户图库
$form->image('field', '字段名')
->server(modstart_web_url('member_data/file_manager/image'));
1
2
3
4
2
3
4
# 多张图片(临时路径) imagesTemp
临时路径指上传到临时表 data_temp
中的图片,正式保存时会将临时图片移动到正式表 data
中。
$form->imagesTemp('field', '字段名');
1
# 链接 link
$form->link('field', '字段名');
1
# 开关 switch
$form->switch('field', '字段名');
// 条件判断
$form->switch('field', '字段名')
->when('=',true, function ($form) {
$form->text('xxx', '开启配置项');
})
->when('=', false,function ($form) {
$form->text('xxx', '关闭配置项');
});
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 多行文本 textarea
$form->textarea('field', '字段名');
1
# 颜色 color
$form->color('field', '字段名');
1
# 日期 date
$form->date('field', '字段名');
1
# 日期时间 datetime
$form->datetime('field', '字段名');
1
# 时间 time
$form->time('field', '字段名');
1
# 单选 radio
$form->radio('field', '字段名');
// 使用数组
$form->radio('field', '字段名')
->options(['a'=>'选项1','b'=>'选项2']);
// 使用BaseType枚举类
$form->radio('field', '字段名')
->optionType(XxxType::class);
// 使用数据表
$form->radio('field', '字段名')
->optionModel('xxxx','id','name');
// 条件
$form->select('field', '字段名')
->when('=', 1, function ($form) {
$form->text('xxx', '条件1配置项');
})
->when('=', 2,function ($form) {
$form->text('xxx', '条件2配置项');
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 下拉 select
// 基本使用
$form->select('field', '字段名');
// 使用Type作为备选项,XxxType是继承BaseType的类
$field->optionType(XxxType::class);
// 使用数组作为备选项
$field->options(['1' => '选项1', '2' => '选项2']);
// 使用数组作为备选项
$field->optionArray([['id' => 1, 'name' => '选项1'], ['id' => 2, 'name' => '选项2']],'id','name');
// 使用模型作为备选项
$field->optionModel('forum_category','id','title');
// 使用模型作为备选项,并渲染为树状结构
$field->optionModelTree('forum_category','id','title');
// 条件
$form->select('field', '字段名')
->when('=', 1, function ($form) {
$form->text('xxx', '条件1配置项');
})
->when('=', 2,function ($form) {
$form->text('xxx', '条件2配置项');
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 富文本 richHtml
// 基础使用
$form->richHtml('field', '字段名');
// 简单版富文本 simple default
$field->editorMode('simple');
// 保存时不经过XSS过滤
// 默认系统保存时会经过XSS过滤,如果是后台应用为了更灵活丰富的样式可选择不过滤XSS
$field->htmlFilter(false);
// 设置富文本后台接口
// 后台默认为 modstart_admin_url('data/ueditor')
// 前台用户默认modstart_web_url('member_data/ueditor')
$field->server('xxx');
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# Markdown markdown
$form->markdown('field', '字段名');
1
# 键值对列表 keyValueList
$form->keyValueList('field', '字段名');
1
# 多值 values
$form->values('field', '字段名');
1
# HTML html
$form->html('field', '字段名');
1
# 数字 number
$form->number('field', '字段名');
1
# 百分比 percent
$form->percent('field', '字段名');
1
# 小数 decimal
$form->decimal('field', '字段名');
1
# 货币 currency
$form->currency('field', '字段名');
1
# ID id
$form->id('field', '字段名');
1
# 验证码 captcha
$form->captcha('field', '字段名');
1
# 临时文件 fileTemp
临时路径指上传到临时表 data_temp
中的图片,正式保存时会将临时图片移动到正式表 data
中。
$form->fileTemp('field', '字段名');
1
# 文件 file
$form->file('field', '字段名');
// 使用用户文件库
$form->file('field', '字段名')
->server(modstart_web_url('member_data/file_manager/file'));
1
2
3
4
2
3
4
# 多文件 files
$form->files('field', '字段名');
// 使用用户文件库
$form->files('field', '字段名')
->server(modstart_web_url('member_data/file_manager/file'));
1
2
3
4
2
3
4
# 视频 video
$form->video('field', '字段名');
// 使用用户视频库
$form->video('field', '字段名')
->server(modstart_web_url('member_data/file_manager/video'));
1
2
3
4
2
3
4
# 音频 audio
$form->audio('field', '字段名');
// 使用用户音频库
$form->audio('field', '字段名')
->server(modstart_web_url('member_data/file_manager/audio'));
1
2
3
4
2
3
4
# 中国地区 areaChina
$form->areaChina('field', '字段名');
1
# 隐藏域 hidden
$form->hidden('field', '字段名');
1
# 图标 icon
$form->icon('field', '字段名');
1