custom-validation.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <el-form
  3. ref="ruleFormRef"
  4. :model="ruleForm"
  5. status-icon
  6. :rules="rules"
  7. label-width="120px"
  8. class="demo-ruleForm"
  9. >
  10. <el-form-item label="Password" prop="pass">
  11. <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
  12. </el-form-item>
  13. <el-form-item label="Confirm" prop="checkPass">
  14. <el-input
  15. v-model="ruleForm.checkPass"
  16. type="password"
  17. autocomplete="off"
  18. />
  19. </el-form-item>
  20. <el-form-item label="Age" prop="age">
  21. <el-input v-model.number="ruleForm.age" />
  22. </el-form-item>
  23. <el-form-item>
  24. <el-button type="primary" @click="submitForm(ruleFormRef)"
  25. >Submit</el-button
  26. >
  27. <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
  28. </el-form-item>
  29. </el-form>
  30. </template>
  31. <script lang="ts" setup>
  32. import { reactive, ref } from 'vue'
  33. import type { FormInstance, FormRules } from 'element-plus'
  34. const ruleFormRef = ref<FormInstance>()
  35. const checkAge = (rule: any, value: any, callback: any) => {
  36. if (!value) {
  37. return callback(new Error('Please input the age'))
  38. }
  39. setTimeout(() => {
  40. if (!Number.isInteger(value)) {
  41. callback(new Error('Please input digits'))
  42. } else {
  43. if (value < 18) {
  44. callback(new Error('Age must be greater than 18'))
  45. } else {
  46. callback()
  47. }
  48. }
  49. }, 1000)
  50. }
  51. const validatePass = (rule: any, value: any, callback: any) => {
  52. if (value === '') {
  53. callback(new Error('Please input the password'))
  54. } else {
  55. if (ruleForm.checkPass !== '') {
  56. if (!ruleFormRef.value) return
  57. ruleFormRef.value.validateField('checkPass', () => null)
  58. }
  59. callback()
  60. }
  61. }
  62. const validatePass2 = (rule: any, value: any, callback: any) => {
  63. if (value === '') {
  64. callback(new Error('Please input the password again'))
  65. } else if (value !== ruleForm.pass) {
  66. callback(new Error("Two inputs don't match!"))
  67. } else {
  68. callback()
  69. }
  70. }
  71. const ruleForm = reactive({
  72. pass: '',
  73. checkPass: '',
  74. age: '',
  75. })
  76. const rules = reactive<FormRules>({
  77. pass: [{ validator: validatePass, trigger: 'blur' }],
  78. checkPass: [{ validator: validatePass2, trigger: 'blur' }],
  79. age: [{ validator: checkAge, trigger: 'blur' }],
  80. })
  81. const submitForm = (formEl: FormInstance | undefined) => {
  82. if (!formEl) return
  83. formEl.validate((valid) => {
  84. if (valid) {
  85. console.log('submit!')
  86. } else {
  87. console.log('error submit!')
  88. return false
  89. }
  90. })
  91. }
  92. const resetForm = (formEl: FormInstance | undefined) => {
  93. if (!formEl) return
  94. formEl.resetFields()
  95. }
  96. </script>