动态循环的el-form做用户角色所属表单验证

 

上图我们只针对表格中某某部门的验证,form表单根据实际情况(后端返回数据中部门的个数)做动态渲染,这里注意form在for循环种嵌套,所以渲染结构必定有多个form,好在表单每一项的验证规则相同

1、后端数据结构

let evaluationQuery = 
{
    "code": 200,
    "success": true,
    "data": {
        "id": "1510177245100482561",
        "tenantId": "",
        "entId": "1506827625228644354",
        "type": 2,
        "status": 1,
        "score": -1,
        "projectName": "项目名称",
        "projectBegin": "2021-04-02 00:00:00",
        "projectEnd": "2022-04-02 00:00:00",
        "createUser": -1,
        "createUserName": "",
        "createTime": "",
        "updateTime": "",
        "contents": [
            {
                "id": "1510177245134036994",
                "tenantId": "000000",
                "entId": "1506827625228644354",
                "evaluationId": "1510177245100482561",
                "deptId": "1506475214370418689",
                "deptName": "生产技术部",
                "weights": 0.5,
                "type": 0,
                "score": -1,
                "content": "",
                "createUser": "1505844651900948481",
                "createUserName": "李三",
                "createTime": "2022-04-02 16:42:11",
                "updateTime": "2022-04-02 16:42:11"
            },
            {
                "id": "1510177245159202818",
                "tenantId": "000000",
                "entId": "1506827625228644354",
                "evaluationId": "1510177245100482561",
                "deptId": "1506475471086989314",
                "deptName": "市场营销部",
                "weights": 0.5,
                "type": 0,
                "score": -1,
                "content": "",
                "createUser": "1505844651900948481",
                "createUserName": "李三",
                "createTime": "2022-04-02 16:42:11",
                "updateTime": "2022-04-02 16:42:11"
            }
        ]
    },
    "msg": "操作成功"
}

 evaluationQuery.contents为部门数据,提交表单入参格式与之相同,所以直接双向绑定拿来用

<tr v-for="(item,index) in evaluationQuery.contents" :key="item.id">
                  <td>{{item.deptName}}</td>
                  <td class="department" colspan=3>

                    <el-form :model="item" :rules="rules" ref="ruleForm">
                        <div class="Conent">
                          <el-form-item prop="type">
                            <el-select :disabled='userInfo.dept_id !== item.deptId' v-model="item.type" placeholder="请选择评价分类">
                              <!-- disabled如果该部门与用户是同一个部门 -->
                              <el-option
                                  v-for="item in evaluationType"
                                  :key="item.dictValue"
                                  :label="item.dictValue"
                                  :value="item.dictKey">
                              </el-option>
                            </el-select>
                          </el-form-item>
                          <el-form-item prop="score">
                            <el-input validate-event :disabled='userInfo.dept_id !== item.deptId' v-model="item.score" placeholder="请输入评分,不超过100分"></el-input>
                          </el-form-item>
                        </div>
                      <el-form-item prop="content">
                        <el-input
                          :disabled='userInfo.dept_id !== item.deptId'
                          type="textarea"
                          placeholder="请输入评论内容"
                          v-model="item.content"
                          maxlength="1000"
                          :autosize="{ minRows: 2, maxRows: 4}"
                          show-word-limit
                          >
                        </el-input>
                      </el-form-item>
                    </el-form>
                  </td>
              </tr>

 经过遍历得到多组from表单,如下图,此时所有表单ref值都相同,也就是说如果现在log得到一数组

console.log(this.$refs.ruleForm);
//结果为数组,长度为3


此时已不能使用原本的this.$refs.ruleForm.validate((valid) => { })去做验证,因为这里我们只验证登录用户user所属部门,假如用户属于企业管理部,那么只校验他对应的表单内容,其他的也是不可编辑的,这是我们通过用户的userInfo信息中所属部门id与后端相应数据中的部门id进行匹配,得到该部门ref表单的索引index,最终写为this.$refs.ruleForm[索引值].validate((valid) => { })去做单一表单验证

假如某一用户存在多个部门,这里方法暂不适用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在使用el-form-item循环表单校验时,可以考虑以下方法。首先,确保在el-form中绑定的是正确的form数据,如引用所示。然后,在循环遍历表单项的时候,需要在每个el-form-item上设置唯一的prop值,用于校验规则的匹配。接着,可以在data中定义一个rules对象,用于存储不同prop值对应的校验规则。这样,就可以根据不同的业务需求,动态地改变校验规则。最后,在el-form-item中使用v-if或v-show指令来显示或隐藏错误提示信息,根据表单项的校验状态来判断是否显示校验错误信息。通过以上方法,可以实现el-form-item循环表单校验的功能,避免了在循环遍历表单时无法正确校验的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [VUE—el-form包含循环遍利的数据校验](https://blog.csdn.net/weixin_43691818/article/details/121923444)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [vue elementui el-form rules动态验证的实例代码详解](https://download.csdn.net/download/weixin_38670297/12941851)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

割凹高

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值