动态增减表单--多个输入框校验

动态增减表单项里一层for循环里多个输入框校验

// 数据结构:dynamicValidateForm:{domains: [{msg:'',content:''},....]}
// model绑定的必须是个对象
<a-form-model
   :labelCol="{ span: 12 }"
    ref="dynamicValidateForm"
    @submit="handleSubmit"
    :model="dynamicValidateForm"
>
  <a-col v-for="(domain, index) in dynamicValidateForm.domains" :key="index">
     // prop字符串,必须是循环的key值(例如 domains)+index+要校验的值(例如:msg)。
     <a-form-model-item :prop="'domains.' + index + '.msg'" :rules="{required: true,message: '' }">
        <a-input v-model="domain.msg" placeholder="请输入" class="ml-10 mr-10 w-100" />
     </a-form-model-item>
     <a-form-model-item :prop="'domains.' + index + '.content'" :rules="{required: true,message: '' }">
        <a-input v-model="domain.content" placeholder="请输入" class="ml-10 mr-10 w-100" />
     </a-form-model-item>
  </a-col>
</a-form-model>

两层for循环里多个输入框校验

// 数据结构: dynamicValidateForm: {domains:[
//             {domain: [{msg:''},....]},
//             {domain: [{msg:''},....]},
//             {domain: [{msg:''},....]},
//             .....
//           ]},
// model绑定的必须是个对象
<a-form-model
   :labelCol="{ span: 12 }"
    ref="dynamicValidateForm"
    @submit="handleSubmit"
    :model="dynamicValidateForm"
>
  <a-row v-for="(domain, index) in dynamicValidateForm.domains" :key="index">
     <a-col v-for="(item,index2) in domain.domain" :key="index2">
       // prop字符串,拼接
       <a-form-model-item  :prop="'domains.' + index + '.domain.' + index2 + '.msg'" :rules="{required: true,message: ''  }">
          <a-input v-model="item.msg" placeholder="请输入" class="ml-10 mr-10 w-100" />
        </a-form-model-item>
      </a-col>
  </a-row>
</a-form-model>

// 如果需求要输入框报红,输入后马上变正常,validate-status可以实现

rules规则

<a-form-model :rules="formRules" :model="dynamicValidateForm">
  <a-form-model-item v-for="(domain, index) in dynamicValidateForm.domains" :key="index">
    <a-input v-model="domain.score"  placeholder="请输入" class="ml-10 mr-10 w-100" />
  </a-form-model-item>
</a-form-model

// formRules要和dynamicValidateForm.domains,结构一一对应,才能正常校验
formRules:{
	// domains是重点,0是变量,score是要校验的对象
	'domains.0.score': [
        { required: true, message: '请输入积分', trigger: 'blur' }
     ],
}
### Vue3 和 Element-Plus 结合使用的表单示例 以下是基于 Vue3 和 Element-Plus 的表单验证实现方法,涵盖了基本配置以及动态校验逻辑。 #### 基本表单结构 通过 `el-form` 组件可以创建一个带有验证功能的表单。该组件支持嵌套字段、自定义规则等功能[^1]。 ```vue <template> <el-form :model="form" :rules="rules" ref="ruleFormRef"> <!-- 用户名输入框 --> <el-form-item label="用户名" prop="username"> <el-input v-model="form.username"></el-input> </el-form-item> <!-- 密码输入框 --> <el-form-item label="密码" prop="password"> <el-input type="password" v-model="form.password"></el-input> </el-form-item> <!-- 提交按钮 --> <el-form-item> <el-button type="primary" @click="submitForm">提交</el-button> </el-form-item> </el-form> </template> ``` #### 数据模型与验证规则 在脚本部分定义数据模型和对应的验证规则: ```javascript <script setup> import { reactive, ref } from 'vue'; import { ElMessage } from 'element-plus'; const form = reactive({ username: '', password: '' }); // 定义验证规则 const rules = { username: [ { required: true, message: '请输入用户名', trigger: 'blur' }, { min: 3, max: 10, message: '长度应在 3 到 10 字符之间', trigger: 'blur' } ], password: [ { required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, message: '密码至少为 6 位字符', trigger: 'blur' } ] }; // 获取 el-form 实例 const ruleFormRef = ref(null); // 提交函数 const submitForm = () => { if (!ruleFormRef.value) return; ruleFormRef.value.validate((valid) => { if (valid) { ElMessage.success('表单验证成功'); } else { ElMessage.error('请修正错误后再提交'); } }); }; </script> ``` 上述代码展示了如何设置简单的表单项及其验证规则,并提供了一个触发验证的方法[^2]。 #### 动态表单扩展 对于更复杂的场景(如动态增减表单项),可以通过数组绑定的方式管理多个子项的数据源。例如,在格中添加条记录并逐一验证其合法性[^3]。 ```vue <template> <el-table :data="tableData" border stripe> <el-table-column label="操作"> <template #default="{ row, $index }"> <el-form-item :prop="'tableData.'+$index+'.value'" :rules="rowRules"> <el-input v-model.number="row.value"></el-input> </el-form-item> </template> </el-table-column> </el-table> </template> ``` 以上片段说明了如何将表单验证集成到格单元格内,从而满足复杂业务需求。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值