当前位置: 首页 > 知识库问答 >
问题:

vue3 - element-plus表单验证?

小牛22988
2024-06-26

下面这段代码来自于element-plus官网,有几点我不明白。

1.这里为什么用setTimeout?有必要吗?它是在失焦的时候才会触发验证

const checkAge = (rule: any, value: any, callback: any) => {  if (!value) {    return callback(new Error('Please input the age'))  }  setTimeout(() => {    if (!Number.isInteger(value)) {      callback(new Error('Please input digits'))    } else {      if (value < 18) {        callback(new Error('Age must be greater than 18'))      } else {        callback()      }    }  }, 1000)}

2.这里的ruleFormRef.value.validateField('checkPass')有必要吗?在失焦之后,checkpass字段不也会触发验证吗?如果设置change时校验,这还是有用的。

const validatePass = (rule: any, value: any, callback: any) => {  if (value === '') {    callback(new Error('Please input the password'))  } else {    if (ruleForm.checkPass !== '') {      if (!ruleFormRef.value) return      ruleFormRef.value.validateField('checkPass')    }    callback()  }}

3.这里的rules有必要为响应式数据吗?

const rules = reactive<FormRules<typeof ruleForm>>({  pass: [{ validator: validatePass, trigger: 'blur' }],  checkPass: [{ validator: validatePass2, trigger: 'blur' }],  age: [{ validator: checkAge, trigger: 'blur' }],})

全部代码:

<template>  <el-form    ref="ruleFormRef"    style="max-width: 600px"    :model="ruleForm"    status-icon    :rules="rules"    label-width="auto"    class="demo-ruleForm"  >    <el-form-item label="Password" prop="pass">      <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />    </el-form-item>    <el-form-item label="Confirm" prop="checkPass">      <el-input        v-model="ruleForm.checkPass"        type="password"        autocomplete="off"      />    </el-form-item>    <el-form-item label="Age" prop="age">      <el-input v-model.number="ruleForm.age" />    </el-form-item>    <el-form-item>      <el-button type="primary" @click="submitForm(ruleFormRef)">        Submit      </el-button>      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>    </el-form-item>  </el-form></template><script lang="ts" setup>import { reactive, ref } from 'vue'import type { FormInstance, FormRules } from 'element-plus'const ruleFormRef = ref<FormInstance>()const checkAge = (rule: any, value: any, callback: any) => {  if (!value) {    return callback(new Error('Please input the age'))  }  setTimeout(() => {    if (!Number.isInteger(value)) {      callback(new Error('Please input digits'))    } else {      if (value < 18) {        callback(new Error('Age must be greater than 18'))      } else {        callback()      }    }  }, 1000)}const validatePass = (rule: any, value: any, callback: any) => {  if (value === '') {    callback(new Error('Please input the password'))  } else {    if (ruleForm.checkPass !== '') {      if (!ruleFormRef.value) return      ruleFormRef.value.validateField('checkPass')    }    callback()  }}const validatePass2 = (rule: any, value: any, callback: any) => {  if (value === '') {    callback(new Error('Please input the password again'))  } else if (value !== ruleForm.pass) {    callback(new Error("Two inputs don't match!"))  } else {    callback()  }}const ruleForm = reactive({  pass: '',  checkPass: '',  age: '',})const rules = reactive<FormRules<typeof ruleForm>>({  pass: [{ validator: validatePass, trigger: 'blur' }],  checkPass: [{ validator: validatePass2, trigger: 'blur' }],  age: [{ validator: checkAge, trigger: 'blur' }],})const submitForm = (formEl: FormInstance | undefined) => {  if (!formEl) return  formEl.validate((valid) => {    if (valid) {      console.log('submit!')    } else {      console.log('error submit!')    }  })}const resetForm = (formEl: FormInstance | undefined) => {  if (!formEl) return  formEl.resetFields()}</script>

共有1个答案

夏景同
2024-06-26

案例的作用是让你知道有哪些用法,真正使用中需要结合自己的业务去做改变,单独的一段代码问合不合适、有没有必要没有意义。

1、这里的setTimeout应该是模拟异步请求,告诉使用者callback可以延迟返回。他下面的TIP也推荐了一种async-validator
2、validatePass是校验Password密码的,里面ruleFormRef.value.validateField('checkPass')是校验Confirm确认密码的,适用的业务场景是,在密码修改完成后,重新校验确认密码与密码是否符合校验规则,比如是否一致
3、rules可以不是响应式,有没有必要看具体业务

 类似资料:
  • 使用 vite 打包组件库,在新的项目中使用时报错 下面是打包后的产物 pe 和 dn 找不到导致项目启动时就报错, 手动再新起一个别名时会结局此问题 vite.config.ts 配置如下 请问这个问题该怎么解决

  • 代码: 进行表单提交校验,打印出来的ruleFormRef.value的值为null。该怎么处理?

  • 系统使用了element-plus按需载入的方式 我需要在一个组件内动态加载某些组件 这个loader方法一直无法正确渲染相应组件,例如type传入“ELInput”时系统会报个警告 尝试过import('element-plus/lib/components/ElInput')这种写法页不行,报错:[plugin:vite:import-analysis] No known conditions

  • 官方续作——Vue 3.0 组件库 Element Plus,由饿了么大前端团队开源出品的一套为开发者、设计师和产品经理准备的基于 Vue 3.0 的组件库,提供了配套设计资源,帮助你的网站快速成型。 Element Plus 使用 TypeScript + Composition API 进行了重构,主要有:  使用 TypeScript 开发,提供完整的类型定义文件 使用 Vue 3.0 Co

  • VUE3 + element-plus,子组件向父组件发送消息(调用父组件函数没有反应),this.$emit方法,请大佬们帮俺看看,谢谢 全部的代码 百度了好久都怎么管用的,刚学不太懂

  • 请问大佬们,如何点击左侧的el-side的菜单,右侧el-main就生成对应的路由标签栏el-tag,每个标签对应一个路由页面,点击该标签可以进入该路由页面???

  • vue3-element-admin 是基于 vue-element-admin 升级的 Vue3 + Element Plus 版本的后台管理框架,是有来技术团队继 youlai-mall 开源全栈商城项目的又一开源力作。 项目使用 Vue3 + Vite2 + TypeScript + Element Plus + Vue Router + Pinia + Volar 等前端主流技术栈,基于此

  • 请问各位大佬们 如何通过vue3+element-plus实现el-table的子列表懒加载???该如何实现点击左侧的箭头实现子节点的懒加载?不然数据多的情况下 会导致卡顿。求指教,谢谢!!! 这是后台返回的数据格式: