企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
![](https://img.kancloud.cn/0f/c1/0fc1a30493050a0d0368b22911f2c5b5_623x752.png) 表单构建组件中表单视图是基于`layui`和`angular`交互构建。 ## 获取构建器实例 1. `ViewBuilder`中获取 ```php $viewBuilder = $this->viewBuilder; $formInstance = $viewBuilder->form; ``` 2. `instance()`自身方法 ```php $formBuilder= FormBuilder::instance(); ``` 3. `new`关键词 ```php $formBuilder= new FormBuilder([ 'id' => '__admin_form', ]); ``` ## 示例1: ~~~ /** * @return string * @throws \Throwable * @throws \builder\base\InvalidInstanceException * @throws \builder\base\NotFoundParamsException */ public function actionAdd() { if ($this->isPost) { // return $this->as403('登录已失效!'); return $this->asOk('删除成功!'); // return $this->asFail('删除失败, 参数错误!'); } else { $get = $this->filteredGet; $viewBuilder = $this->viewBuilder; $viewBuilder->setPartial(); $form = $viewBuilder->form; $form->registerPointcut(H5::alert('我是表单切点', 'info', ['class' => ['mb-0']])) ->setFormControl([ 'j' => $this->formControl->textarea()->label('文本域')->required()->defaultValue('卧槽我\r\n卧槽"\'' . PHP_EOL), 'k' => $this->formControl->checkbox()->defaultValue(['a'])->options(['a' => '我', 'b' => '是', 'c' => '神',])->required()->label('标签')->tag(), 'l' => $this->formControl->checkbox()->defaultValue(['e', 'f'])->options(['d' => '我', 'e' => '是', 'f' => '神',])->required()->label('多选'), 'm' => $this->formControl->sw()->label('开关')->required()->checked(), 'n' => $this->formControl->hidden()->defaultValue('冲冲冲'), 'o' => $this->formControl->password()->label('密码')->required()->defaultValue('123456'), 'p' => $this->formControl->radio()->options(['g' => '我', 'h' => '是', 'i' => '神',])->defaultValue('h')->required()->label('单选'), 'q' => $this->formControl->range()->required()->label('范围')->defaultValue(1), ]) ->setCloseBtn() ->setSubmitBtn() ->setSubmittedCalls([ 'parent' => isset($get['_bid']) ? $get['_bid'] : null, ]) ->setAutoClose(true); return $viewBuilder->render(); } } ~~~ ## 示例 2 ~~~ <?php /** * @link https://ym2.cleverstone.top * @copyright Copyright (c) 2020 Yii Manager Software LLC */ namespace backend\controllers; use builder\base\BaseController; use builder\form\FormBuilder; use builder\form\util\control\trigger\Trigger; use builder\form\util\sredirect\Modal; use builder\form\util\sredirect\Page as PPPP; use builder\helper\D; use builder\helper\H5; use \builder\form\util\control\dselect\options\Modal as DselectModal; use \builder\form\util\control\dselect\options\Page as DselectPage; use builder\helper\Url; /** * 表单构建器 * @author cleverstone * @since ym2.0 */ class FormController extends BaseController { public $guestActions = [ 'index', 'link', 'vlink', 'vcompute', 'dynamic-select', 'addition', 'dynamic-control', 'tree', 'remote', ]; public function getData() { return [ ['name' => 'name1', 'value' => 1, 'children' => []], ['name' => 'name2', 'value' => 2, 'children' => []], ['name' => 'name3', 'value' => 3, 'children' => []], ['name' => 'name4', 'value' => 4, 'children' => []], ['name' => 'name5', 'value' => 5, 'children' => []], ['name' => 'name6', 'value' => 6, 'children' => []], ['name' => 'name7', 'value' => 7, 'children' => []], ]; } public function getTreeData() { return [ ['name' => 'name1', 'value' => 1, 'children' => [ ['name' => 'name1-2', 'value' => 12, 'children' => []], ['name' => 'name1-3', 'value' => 13, 'children' => []], ['name' => 'name1-4', 'value' => 14, 'children' => []], ['name' => 'name1-5', 'value' => 15, 'children' => []], ['name' => 'name1-6', 'value' => 16, 'children' => []], ['name' => 'name1-7', 'value' => 17, 'children' => []], ]], ['name' => 'name2', 'value' => 2, 'children' => []], ['name' => 'name3', 'value' => 3, 'children' => []], ['name' => 'name4', 'value' => 4, 'children' => [ ['name' => 'name4-5', 'value' => 45, 'children' => []], ['name' => 'name4-6', 'value' => 46, 'children' => []], ['name' => 'name4-7', 'value' => 47, 'children' => []], ]], ['name' => 'name5', 'value' => 5, 'children' => []], ['name' => 'name6', 'value' => 6, 'children' => []], ['name' => 'name7', 'value' => 7, 'children' => []], ]; } public function actionRemote() { $data = [ ['name' => 'name1', 'value' => 1, 'children' => [ ['name' => 'name1-2', 'value' => 12, 'children' => []], ['name' => 'name1-3', 'value' => 13, 'children' => []], ['name' => 'name1-4', 'value' => 14, 'children' => []], ['name' => 'name1-5', 'value' => 15, 'children' => []], ['name' => 'name1-6', 'value' => 16, 'children' => []], ['name' => 'name1-7', 'value' => 17, 'children' => []], ]], ['name' => 'name2', 'value' => 2, 'children' => []], ['name' => 'name3', 'value' => 3, 'children' => []], ['name' => 'name4', 'value' => 4, 'children' => [ ['name' => 'name4-5', 'value' => 45, 'children' => []], ['name' => 'name4-6', 'value' => 46, 'children' => []], ['name' => 'name4-7', 'value' => 47, 'children' => []], ]], ['name' => 'name5', 'value' => 5, 'children' => []], ['name' => 'name6', 'value' => 6, 'children' => []], ['name' => 'name7', 'value' => 7, 'children' => []], ]; return $this->asOk('success', $data); } /** * @return string * @throws \yii\base\InvalidConfigException */ public function actionTree() { if ($this->isPost) { return $this->asOk('success'); } else { $formBuilder = FormBuilder::instance(); $formBuilder->setTitle('xm-select') ->setFormControl([ 'a' => $this->formControl->xselect()->label('xm-select')->defaultValue([1])->data($this->getData())->tips('请选择一哈!')->filterable()->radio()->clickClose(), 'd' => $this->formControl->xselectTree() ->label('xm-select-tree') ->defaultValue(['6']) ->data($this->getTreeData()) ->tips('请选择一哈!') ->filterable() //->radio() //->strict(false) ->expandedKeys(true) ->addPluginOption('height', '500px') /*->clickClose()*/, 'e' => $this->formControl->xselectRemote() ->label('xm-select-remote') ->tips('请选择一哈!') ->filterable() ->radio() ->clickClose() ->toolbar([ 'show' => true, ]) ->addPluginOption('tree', [ 'show' => true, ]) ->url(Url::to('form/remote', '')) ->httpMethod('get'), 'f' => $this->formControl->xselectCascader() ->label('xm-select-cascader') ->defaultValue(['6']) ->data($this->getTreeData()) ->tips('请选择一哈!') ->radio() ->clickClose() ->strict(false), 'g' => $this->formControl->file() ->label('测试一下') ->max(5) ->scenarioImage() ->scenarioVideo() ->scenarioAudio() ->scenarioOther() ->defaultValue('https://tfs.alipayobjects.com/images/partner/TB1TrI3cTVyDuNk6XeaXXXCWXXa'), ]) ->setResetBtn() ->setSubmitBtn(); return $formBuilder->render(); } } /** * @return string * @throws \Throwable */ public function actionIndex() { if ($this->isPost) { return $this->asOk('success', [ 'a' => '1555' ]); } else { $form = FormBuilder::instance([ 'id' => 'formDemo', ]); $form->setTitle('表单构建器') ->setRootLayout([ 'class' => ['col-md-12'] ]) ->setPartial(false) ->setRequiredStyle() ->registerPointcut(H5::alert('我是一个信息提示!')) ->setFormControl([ 'b' => $this->formControl->password()->label('密码')->placeholder('请填写密码')->comment('注:密码必须包含特殊字符,且大于6位小于22位'), 'i' => $this->formControl->select()->label('触发器')->options(['a' => '触发A', 'b' => '触发B']), 'aa' => $this->formControl->xselect()->label('xselect')->data([ ['name' => 'n1', 'value' => 1], ['name' => 'n2', 'value' => 2], ['name' => 'n3', 'value' => 3], ['name' => 'n4', 'value' => 4], ['name' => 'n5', 'value' => 5], ])->radio()->clickClose(), 'n' => $this->formControl->sw()->label('开关')->checked(), 'trigger1' => $this->formControl->trigger()->when('default', 'i', '!=', 'b')->andWhen('default', 'aa', '==', 3)->triggerItems([ 'j' => $this->formControl->text()->label('触发器1-1'), 'k' => $this->formControl->text()->label('触发器1-2'), 'trigger3' => $this->formControl->trigger()->when('default', 'n', '==', '0')->triggerItems([ 'o' => $this->formControl->text()->label('内部触发器1-1'), 'p' => $this->formControl->text()->label('内部触发器1-2'), ])->display(), ])->display(), 'trigger2' => $this->formControl->trigger()->when('default', 'i', '==', 'b')->triggerItems([ 'l' => $this->formControl->text()->label('触发器2-1'), 'm' => $this->formControl->text()->label('触发器2-2'), ])->display(), 'e' => $this->formControl->select()->label('分组')->multiple()->disabled(['A', 'E'])->options([ '会员列表' => [ 'A' => '数字1', 'B' => '数字2', 'C' => '数字3', ], '日志列表' => [ 'D' => '日志1', 'E' => '日志2', 'F' => '日志3', ], ]), 'f' => $this->formControl->selectLink()->label('省')->disabled(['A', 'E'])->initOptions([ '中国' => [ 'A' => '河南省', 'B' => '广东省', 'C' => '北京市', ], '美国' => [ 'D' => '华盛顿', 'E' => '剑桥', 'F' => '麻省理工', ], ])->targetLink('g')->route('form/link')->start(), 'g' => $this->formControl->selectLink()->label('市')->targetLink('h')->route('form/link')->through(), 'h' => $this->formControl->selectLink()->label('县(区)')->end(), ]) ->setSubmitConfirm() ->setExtraBtn([ H5::tag('button', '额外的按钮', ['class' => ['layui-btn layui-btn-sm layui-btn-gray ym2-form-extra-ui'], 'type' => 'button']), ]) ->setSubmitBtn('确定') ->setResetBtn() ->setExtraJs([ function () { return "console.log('额外的JS')"; }, ]) ->setSubmittedRedirect(function (Modal $modal) { return $modal->route('form/vlink')->title('我是狗')->params(['bbbb' => 1, 'c' => ':a'])->closeBtn()->width(800)->height(800); }) ->registerPointcut(H5::alert('我是一个底部信息提示!', 'info'), FormBuilder::POS_END); return $form->render(); } } /** * @return string */ public function actionLink() { $res = [ '中国' => [ ['value' => 'D', 'text' => '郑州市', 'disabled' => true], ['value' => 'E', 'text' => '东莞市'], ['value' => 'F', 'text' => '北京市'], ], '美国' => [ ['value' => 'G', 'text' => '西安市', 'disabled' => true], ['value' => 'H', 'text' => '新乡市'], ['value' => 'I', 'text' => '洛阳市'], ], ]; $res2 = [ ['value' => 'D', 'text' => '郑州市', 'disabled' => false], ['value' => 'E', 'text' => '东莞市'], ['value' => 'F', 'text' => '北京市'], ['value' => 'G', 'text' => '新乡市'], ['value' => 'H', 'text' => '洛阳市'], ['value' => 'I', 'text' => '开封市'], ['value' => 'IE', 'text' => '开封市2222'], ]; return $this->asOk('success', $res2); } /** * @return string * @throws \Throwable */ public function actionVlink() { if ($this->isPost) { return $this->asOk('success'); } else { $form = FormBuilder::instance(); $form->setTitle('表单值关联') ->setRequiredStyle() ->setFormControl([ 'c' => $this->formControl->text()->label('普通控件'), 'ccc' => $this->formControl->time()->label('日期')->range(1), 'valueLink1' => $this->formControl->valueLink()->sourceControl([ 'b' => $this->formControl->text()->label('值源1'), 'e' => $this->formControl->text()->label('值源2'), ])->targetRoute(['form/vcompute', 'eeeee' => 2000])->targetField('d'), 'd' => $this->formControl->staticText()->label('值连接1')->defaultValue('0'), 'valueLink2' => $this->formControl->valueLink()->sourceControl([ 'f' => $this->formControl->text()->label('值源3'), 'g' => $this->formControl->text()->label('值源4'), ])->targetRoute('form/vcompute')->targetField('h'), 'h' => $this->formControl->staticText()->label('值连接2')->defaultValue('0'), 'valueLink3' => $this->formControl->valueLink()->sourceControl([ 'i' => $this->formControl->xselect()->data([ ['name' => 'name1', 'value' => 1], ['name' => 'name2', 'value' => 2], ['name' => 'name3', 'value' => 3], ['name' => 'name4', 'value' => 4], ])->radio()->clickClose(), 'j' => $this->formControl->text()->label('值源6'), ])->targetRoute('form/vcompute')->targetField('k'), 'k' => $this->formControl->staticText()->label('值连接3')->defaultValue('0'), ]) ->setResetBtn() ->setSubmitBtn(); !empty($this->get['bbbb']) && $form->setPartial(); return $form->render(); } } /** * @return string */ public function actionVcompute() { $post = $this->filteredPost; unset($post['_ym2_csrf_backend']); if (empty($post)) { return $this->asOk('success', '0'); } return $this->asOk('success', microtime(true)); } /** * @return string * @throws \ReflectionException * @throws \builder\base\UndefinedParamsException * @throws \yii\base\InvalidConfigException */ public function actionDynamicSelect() { if ($this->isPost) { return $this->asOk('提交成功'); } else { $formBuilder = FormBuilder::instance(); $formBuilder->setTitle('动态下拉框') ->setRequiredStyle() ->setFormControl([ 'a' => $this->formControl->text()->label('普通文本'), 'b' => $this->formControl->selectDynamic() ->label('动态下拉1') ->layout(12) ->required() ->placeholder('请填写一下') ->defaultValue(['F']) ->multiple() //->refreshBtn() //->refreshBtnPosition() ->additionBtn(function (DselectModal $modal) { return $modal->route('form/addition')->title('新增')->height(230); }) //->additionBtnPosition() ->dataUrl('form/link') ->comment(''), ]) ->setResetBtn() ->setSubmitBtn(); return $formBuilder->render(); } } /** * @return string * @throws \yii\base\InvalidConfigException */ public function actionAddition() { if ($this->isPost) { return $this->asOk('success'); } else { $formBuilder = FormBuilder::instance(); $formBuilder->setPartial() ->setFormControl([ 'a' => $this->formControl->text()->label('CS'), ]) ->setSubmitBtn() ->setAutoClose() ->setSubmittedCalls(['parent' => null]); return $formBuilder->render(); } } /** * @return string * @throws \Throwable */ public function actionDynamicControl() { if ($this->isPost) { return $this->asOk('表单提交成功'); } else { $formBuilder = FormBuilder::instance(); $formBuilder->setTitle('表单动态控件') ->setGroup('会员中心', 'center') ->setGroupCol(4) ->setFormControl([ 'user' => $this->formControl->text()->label('用户名'), 'member1' => $this->formControl ->dynamicControl() ->label('动态控件1') ->comment('注:这是注释') ->allowDragSorting() ->allowDefaultDrop() ->layout(12) ->items([ 'm' => $this->dynamicControl->file() ->label('测试一下啊') ->scenarioImage() ->scenarioVideo() ->scenarioOther() ->max(1) ->defaultValue(['https://tfs.alipayobjects.com/images/partner/TB1TrI3cTVyDuNk6XeaXXXCWXXa']) ->style(['width' => '50px', 'height' => '50px']), 'a' => $this->dynamicControl->checkbox()->label('多选')->options(['1' => '男', '2' => '女'])->required()->minWidth(150), 'd' => $this->dynamicControl->month()->label('月')->required(), 'e' => $this->dynamicControl->number()->label('数字')->required(), 'f' => $this->dynamicControl->password()->label('密码')->required(), // 'g' => $this->dynamicControl->radio()->label('单选')->options(['1' => '男', '2' => '女'])->required()->minWidth(200), // 'h' => $this->dynamicControl->range()->label('滑块')->required(), // 'i' => $this->dynamicControl->section()->label('区间')->required()->firstStyle('width:60px;')->lastStyle('width:60px;')->minWidth(200), // 'j' => $this->dynamicControl->select()->label('下拉选择')->options(['1' => '男', '2' => '女'])->required()->minWidth(150), // 'k' => $this->dynamicControl->sw()->label('开关')->required(), // 'l' => $this->dynamicControl->text()->label('文本')->required(), ])->data([ [ 'a' => ['1' => 1], 'd' => '2020-02', 'e' => '100', 'f' => '100', // 'g' => '1', // 'h' => 50, // 'i' => ['1', '110'], // 'j' => 1, // 'k' => 1, // 'l' => '文本', ], [ 'a' => ['2' => 1], 'd' => '2020-02', 'e' => '100', 'f' => '100', // 'g' => '2', // 'h' => 50, // 'i' => ['1', '110'], // //'j' => 2, // 'k' => 0, // 'l' => '文本', ], ]), 'member2' => $this->formControl ->dynamicControl() ->label('动态控件2') ->comment('注:这是注释') ->allowDragSorting() ->allowDefaultDrop() ->layout(12) ->items([ 'a' => $this->dynamicControl->checkbox()->label('多选')->options(['1' => '男', '2' => '女'])->required()->minWidth(150), 'd' => $this->dynamicControl->month()->label('月')->required(), 'e' => $this->dynamicControl->number()->label('数字')->required(), 'f' => $this->dynamicControl->password()->label('密码')->required(), // 'g' => $this->dynamicControl->radio()->label('单选')->options(['1' => '男', '2' => '女'])->required()->minWidth(200), // 'h' => $this->dynamicControl->range()->label('滑块')->required(), // 'i' => $this->dynamicControl->section()->label('区间')->required()->firstStyle('width:60px;')->lastStyle('width:60px;')->minWidth(200), // 'j' => $this->dynamicControl->select()->label('下拉选择')->options(['1' => '男', '2' => '女'])->required()->minWidth(150), // 'k' => $this->dynamicControl->sw()->label('开关')->required(), // 'l' => $this->dynamicControl->text()->label('文本')->required(), ])->data([ [ 'a' => ['1' => 1], 'd' => '2020-02', 'e' => '100', 'f' => '100', // 'g' => '1', // 'h' => 50, // 'i' => ['1', '110'], // 'j' => 1, // 'k' => 1, // 'l' => '文本', ], [ 'a' => ['2' => 1], 'd' => '2020-02', 'e' => '100', 'f' => '100', // 'g' => '2', // 'h' => 50, // 'i' => ['1', '110'], // //'j' => 2, // 'k' => 0, // 'l' => '文本', ], ]), ]) ->setGroup('用户中心', 'user') ->setGroupCol(4) ->setFormControl([ 'user' => $this->formControl->text()->label('用户名'), 'member1' => $this->formControl ->dynamicControl() ->label('动态控件3') ->comment('注:这是注释') ->allowDragSorting() ->allowDefaultDrop() ->layout(12) ->items([ 'a' => $this->dynamicControl->checkbox()->label('多选')->options(['1' => '男', '2' => '女'])->required()->minWidth(150), 'd' => $this->dynamicControl->month()->label('月')->required(), 'e' => $this->dynamicControl->number()->label('数字')->required(), 'f' => $this->dynamicControl->password()->label('密码')->required(), // 'g' => $this->dynamicControl->radio()->label('单选')->options(['1' => '男', '2' => '女'])->required()->minWidth(200), // 'h' => $this->dynamicControl->range()->label('滑块')->required(), // 'i' => $this->dynamicControl->section()->label('区间')->required()->firstStyle('width:60px;')->lastStyle('width:60px;')->minWidth(200), // 'j' => $this->dynamicControl->select()->label('下拉选择')->options(['1' => '男', '2' => '女'])->required()->minWidth(150), // 'k' => $this->dynamicControl->sw()->label('开关')->required(), // 'l' => $this->dynamicControl->text()->label('文本')->required(), ])->data([ [ 'a' => ['1' => 1], 'd' => '2020-02', 'e' => '100', 'f' => '100', // 'g' => '1', // 'h' => 50, // 'i' => ['1', '110'], // 'j' => 1, // 'k' => 1, // 'l' => '文本', ], [ 'a' => ['2' => 1], 'd' => '2020-02', 'e' => '100', 'f' => '100', // 'g' => '2', // 'h' => 50, // 'i' => ['1', '110'], // //'j' => 2, // 'k' => 0, // 'l' => '文本', ], ]), 'member2' => $this->formControl ->dynamicControl() ->label('动态控件4') ->comment('注:这是注释') ->allowDragSorting() ->allowDefaultDrop() ->layout(12) ->items([ 'a' => $this->dynamicControl->checkbox()->label('多选')->options(['1' => '男', '2' => '女'])->required()->minWidth(150), 'd' => $this->dynamicControl->month()->label('月')->required(), 'e' => $this->dynamicControl->number()->label('数字')->required(), 'f' => $this->dynamicControl->password()->label('密码')->required(), // 'g' => $this->dynamicControl->radio()->label('单选')->options(['1' => '男', '2' => '女'])->required()->minWidth(200), // 'h' => $this->dynamicControl->range()->label('滑块')->required(), // 'i' => $this->dynamicControl->section()->label('区间')->required()->firstStyle('width:60px;')->lastStyle('width:60px;')->minWidth(200), // 'j' => $this->dynamicControl->select()->label('下拉选择')->options(['1' => '男', '2' => '女'])->required()->minWidth(150), // 'k' => $this->dynamicControl->sw()->label('开关')->required(), // 'l' => $this->dynamicControl->text()->label('文本')->required(), ])->data([ [ 'a' => ['1' => 1], 'd' => '2020-02', 'e' => '100', 'f' => '100', // 'g' => '1', // 'h' => 50, // 'i' => ['1', '110'], // 'j' => 1, // 'k' => 1, // 'l' => '文本', ], [ 'a' => ['2' => 1], 'd' => '2020-02', 'e' => '100', 'f' => '100', // 'g' => '2', // 'h' => 50, // 'i' => ['1', '110'], // //'j' => 2, // 'k' => 0, // 'l' => '文本', ], ]), ]) ->setResetBtn() ->setSubmitBtn(); return $formBuilder->render(); } } } ~~~