element表单验证

  1 <template>
  2   <el-dialog title="修改密码"
  3              :visible.sync="isChangeDialog"
  4              width="23%"
  5              ref="dialog"
  6              :modal-append-to-body='false'
  7              :before-close="close"
  8              class="changepwd"
  9   >
 10     <div class="diolag-details">
 11       <el-form class="user-account-key" ref="form" :model="form" :rules="rules" label-width="100px">
 12         <el-form-item label="原密码" prop="oldpwd">
 13           <el-input placeholder="请输入原密码" v-model="form.oldpwd" @blur="getoldpwd"></el-input>
 14         </el-form-item>
 15         <el-form-item label="新密码" prop="newpwd">
 16           <el-input type="password" placeholder="请设置新密码" v-model="form.newpwd"></el-input>
 17         </el-form-item>
 18         <el-form-item label="确认密码" prop="newpwdagin">
 19           <el-input type="password" placeholder="请确认新密码" v-model="form.newpwdagin"></el-input>
 20         </el-form-item>
 21         <el-form-item class="dialog-footer">
 22           <el-button @click="changePwd('form')">保 存</el-button>
 23           <el-button type="primary" @click="close">取 消</el-button>
 24         </el-form-item>
 25       </el-form>
 26     </div>
 27   </el-dialog>
 28 </template>
 29 
 30 <script>
 31   import {upPassword} from "../../request/login"
 32   export default {
 33     name: "ChangePassword",
 34     data(){
 35       let validateNewPassword = (rule, value, callback) => {
 36         if (value === this.oldpwd) {
 37           callback(new Error('新密码不能与原密码相同!'))
 38         } else {
 39           callback()
 40         }
 41       };
 42       let validateNewPassword2 = (rule, value, callback) => {
 43         if (value !== this.form.newpwd) {
 44           callback(new Error('与新密码不一致!'))
 45         } else {
 46           callback()
 47         }
 48       };
 49       let validateOldPassword = (rule, value, callback) => {
 50         if (value !== this.oldpwd) {
 51           callback(new Error('请输入正确的密码!'))
 52         }else {
 53           callback()
 54         }
 55       };
 56       return{
 57         isChangeDialog:false,
 58         form:{},
 59         oldpwd:'',
 60         userid:'',
 61         newpwd : '',
 62         newpwdagin:'',
 63         rules: {
 64           oldpwd: [
 65             { required: true, message: '请输入原密码', trigger: 'blur' },
 66             { validator: validateOldPassword, trigger: 'blur' }
 67           ],
 68           newpwd: [
 69             { required: true, message: '请设置新密码', trigger: 'blur' },
 70             { validator: validateNewPassword, trigger: 'blur' }
 71           ],
 72           newpwdagin: [
 73             { required: true, message: '请确认新密码', trigger: 'blur' },
 74             { validator: validateNewPassword2, trigger: 'blur' }
 75           ]
 76         }
 77       }
 78     },
 79     methods:{
 80       close(){
 81         this.$store.dispatch('changeUserpwd',false)
 82       },
 83       getoldpwd(){
 84         this.oldpwd = sessionStorage.getItem('pwd')
 85       },
 86       changePwd (formName) {
 87         this.$store.dispatch('changeUserpwd',false);
 88         this.$refs[formName].validate(valid => {
 89           if (valid) {
 90             upPassword(this.userid,this.newpwd).then(res => {
 91               this.$router.push('/Login');
 92               this.$message({
 93                 message:'密码修改成功',
 94                 type:'success'
 95               })
 96             }).catch(err => {
 97               this.$message({
 98                 message:'密码修改失败',
 99                 type:'error'
100               });
101               console.log(err)
102             })
103           } else {
104             this.$message.error('请正确填写表单');
105             return false
106           }
107         });
108         this.$refs[formName].resetFields();
109       },
110     },
111     watch:{
112       '$store.state.changePwd' :function (newVal) {
113         this.isChangeDialog  = newVal;
114       }
115     }
116   }
117 </script>
118 
119 <style lang="scss" scoped>
120   .diolag-details{
121     margin-top: 10px;
122   }
123 </style>
View Code

用的element-dialog,将弹框组件化,所以用vuex来改变弹框的显隐藏。

密码为用户登录时的密码,将其存储在本地,判断表单里的原密码是否与其相同,采用了element自带的表单验证规则

 

在store.js里设置dialog的初始值

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    changePwd:false,
  },
  mutations: {
    changepwd(state,flag){
      state.changePwd = flag
    }
  },
  actions: {
    changeUserpwd({commit},flag){
      commit('changepwd',flag)
    }
  }
})

 

posted @ 2019-10-08 10:12  甜甜的小可爱ha  阅读(315)  评论(0编辑  收藏  举报