表单验证不见效

[复制链接]
发表于 2025-11-16 08:34:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
vue

element-ui

必要注意
:model
:rules
ref
  1. <el-form :model="myFormData" status-icon :rules="rules" ref="myFormData" label-width="100px" class="demo-ruleForm">
  2.   <el-form-item label="姓名" prop="name">
  3.     <el-input type="text" v-model="myFormData.name"></el-input>
  4.   </el-form-item>
  5.   
  6.   <el-form-item label="年龄" prop="age">
  7.     <el-input type="text" v-model="myFormData.age"></el-input>
  8.   </el-form-item>
  9.   
  10.   <el-form-item>
  11.     <el-button type="primary" @click="submitForm()">提交</el-button>
  12.   </el-form-item>
  13.   
  14. </el-form>
复制代码
  1. <script>
  2.   export default {
  3.     data() {
  4.       return {
  5.         myFormData: {
  6.           name: '',
  7.           age: ''
  8.         },
  9.         rules: {
  10.                 name:[
  11.                         {
  12.                                 required: true,
  13.                                 message: '请输入姓名',
  14.                                 trigger:['blur','change']
  15.                         }
  16.                 ],
  17.                 age:[
  18.                         {
  19.                                 required: true,
  20.                                 message: '请输入年龄',
  21.                                 trigger:['blur','change']
  22.                         }
  23.                 ],
  24.         }
  25.       };
  26.     },
  27.     methods: {
  28.       submitForm() {
  29.               this.$refs['myFormData'].validate((valid) => {
  30.                         if (valid) {
  31.                                 // 执行表单验证成功该执行的代码
  32.                         } else {
  33.                                 // 执行表单验证失败应该执行的代码
  34.                         }
  35.                 });
  36.         
  37.       },
  38.     }
  39.   }
  40. </script>
复制代码
uniapp

uni-forms

必要注意
:modelValue
:rules
ref
  1. <template>
  2.         <view>
  3.                 <div class="all">
  4.                         <view class="homework">
  5.                        
  6.                                 <uni-forms :modelValue="myFormData" :rules="rules" ref="myFormData">
  7.                                
  8.                                         <uni-forms-item label="姓名:" name="realName" :required="true">
  9.                                                 <uni-easyinput type='text' v-model="myFormData.realName" placeholder="请输入姓名" />
  10.                                         </uni-forms-item>
  11.                                        
  12.                                         <uni-forms-item label="身份证号码" prop="" name="idNumber" :required="true">
  13.                                                 <uni-easyinput v-model="myFormData.idNumber" placeholder="请输入身份证号码" />
  14.                                         </uni-forms-item>
  15.                                        
  16.                                 </uni-forms>
  17.                                
  18.                         </view>
  19.        
  20.                         <div class="openDevice">
  21.                                 <view class="btn" @click="submit">
  22.                                         提交
  23.                                 </view>
  24.                         </div>
  25.                 </div>
  26.         </view>
  27. </template>
复制代码
  1. <script>
  2.         export default {
  3.                 data() {
  4.                         return {
  5.                                 myFormData: {
  6.                         realName:'',
  7.                         idNumber:''
  8.                     },
  9.                                 rules: {
  10.                                         realName: {
  11.                                                 rules: [{
  12.                                                         required: true,
  13.                                                         errorMessage: '请输入姓名',
  14.                                                 },
  15.                                                 // {
  16.                                                 //         minLength: 3,
  17.                                                 //         maxLength: 5,
  18.                                                 //         errorMessage: '姓名长度在 {minLength} 到 {maxLength} 个字符',
  19.                                                 // }
  20.                                         ]},
  21.                                         idNumber: {
  22.                                                 rules: [{
  23.                                                         required: true,
  24.                                                         errorMessage: '请输入身份证号码',
  25.                                                 },
  26.                                         ]},
  27.                                 },
  28.                         }
  29.                 },
  30.                 onLoad(options) {},
  31.             onShow() {},
  32.                 mounted() {},
  33.                 methods: {
  34.                         submit() {
  35.                                 this.$refs['myFormData'].validate((valid) => {
  36.                                         console.log(valid);
  37.                                     if (!valid) {
  38.                                                 // 验证通过
  39.                                         console.log('验证通过')
  40.                                         this.$emit('tabComponent', this.info);
  41.                                         ElMessage.success("注册成功!")
  42.                                  
  43.                                     } else {
  44.                                                 // 验证不通过
  45.                                                 console.log('验证不通过')
  46.                                         }
  47.                                 })
  48.                    
  49.                     console.log(this.myFormData)
  50.                         },
  51.                 }
  52.         }
  53. </script>
复制代码
  1. <style scoped lang="scss">
  2.         .all {
  3.                 width: 100vw;
  4.                 background-color: #fff;
  5.                 // padding: 30rpx;
  6.                 box-sizing: border-box !important;
  7.                 .news {
  8.                         width: 100vw;
  9.                         height: 513rpx;
  10.                         background: url('../../../static/scan/homeworkBg.png') no-repeat;
  11.                         background-size: 100% 100%;
  12.                         background-attachment: cover;
  13.                         background-color: #37a7f1;
  14.                         display: flex;
  15.                         flex-direction: column;
  16.                         justify-content: space-evenly;
  17.                         padding-left: 80rpx;
  18.                         color: #fff;
  19.                         box-sizing: border-box;
  20.                 }
  21.                 .homework {
  22.                         padding: 70rpx;
  23.                         box-sizing: border-box;
  24.                         overflow: hidden;
  25.                 }
  26.                 .box {
  27.                         position: relative;
  28.                         top: -30px;
  29.                         z-index: 999;
  30.                         background: #fff;
  31.                         width: 100%;
  32.                         border-radius: 40rpx 40rpx 0rpx 0rpx;
  33.                 }
  34.                 ::v-deep .uni-forms-item__label {
  35.                         width: 120px !important;
  36.                         color: #333333;
  37.                         font-weight: 500;
  38.                         font-size: 30rpx;
  39.                 }
  40.                 ::v-deep .uni-forms-item {
  41.                         border-bottom: 1px solid #f4f3f3;
  42.                 }
  43.                 ::v-deep .uni-select {
  44.                         border: none !important;
  45.                 }
  46.                 ::v-deep .is-input-border {
  47.                         border: none !important;
  48.                 }
  49.         .list-cell{
  50.             display: flex;
  51.         }
  52.         .uni-list-cell{
  53.             margin-right: 10rpx;
  54.         }
  55.                 .btn {
  56.                         width: 80%;
  57.                         height: 100rpx;
  58.                         line-height: 100rpx;
  59.                         text-align: center;
  60.                         background: #3F9DFD;
  61.                         border-radius: 50rpx;
  62.                         font-weight: 400;
  63.                         font-size: 32rpx;
  64.                         color: #FFFFFF;
  65.                 }
  66.                 .openDevice {
  67.                         width: 100%;
  68.                         display: flex;
  69.                         justify-content: center;
  70.                 }
  71.                 .text {
  72.                         width: 427rpx;
  73.                         height: 160rpx;
  74.                         font-family: Noto Sans S Chinese;
  75.                         font-weight: normal;
  76.                         font-size: 28rpx;
  77.                         color: #333333;
  78.                 }
  79.         }
  80. </style>
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表