fastadmin中编辑时的fieldlist选项类型,如何跟数据库里的保持一致,并且显示匹配的样式

几个关键点

一:改变显示样式

1.自动加载
应该按数据库里的样式。来展现对应的题型
2.手动改变
触发不同的类型,改变不同的题型
3.如何把多选的选项录入到数据库
一般使用params来当做传入的变量,一般使用rows来当做,输出的变量
因此,录入的时候,要使用
```
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
$type = intval($params['type']) - 1;
$params['selectdata'] = $params['selectdata' . $type];
$selectarr = json_decode($params['selectdata'], true);
$params['selectnumber'] = count($selectarr);
$params['answer'] = $params['answer' . $type];

            foreach ($selectarr as $key => $value) {
                if (empty($value['key']) && $value['key'] != '0') {
                    $this->error("请填写选项" . ($key + 1));
                }
                if (empty($value['value']) && $value['value'] != '0') {
                    $this->error("请填写选项" . ($key + 1) . "答案内容");
                }
                unset($selectarr[$key]['checked']);


            }
            $params['selectdata'] = json_encode($selectarr);


            if (count(array_unique(array_map('strtolower', array_column($selectarr, 'key')))) != count($selectarr)) {
                $this->error("请不要输入重复选项!【选项不区分大小写】");

            }

            if (count(array_unique(array_column($selectarr, 'value'))) != count($selectarr)) {
                $this->error("请不要输入重复选项答案!");

            }
            if ($type == '1') {
                $params['answer'] = implode(',', $params['answer']);
            }


            if (empty($params['answer']) && $params['answer'] != '0') {
                $this->error("请选择正确答案!");
            }
            $result = false;
            Db::startTrans();
            try {
                //是否采用模型验证
                if ($this->modelValidate) {
                    $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                    $row->validateFailException(true)->validate($validate);
                }
                $result = $row->allowField(true)->save($params);
                Db::commit();
            } catch (ValidateException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success();
            } else {
                $this->error(__('No rows were updated'));
            }
        }
        $this->error(__('Parameter %s can not be empty', ''));
    }
4
posted @ 2020-05-21 10:09  风意不止  阅读(2328)  评论(0编辑  收藏  举报