• 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)
        }
      }
    })
  • 相关阅读:
    layui弹出层:使用icon图标小结
    layui弹出层:使用icon图标小结
    存储过程分页
    .net 更改GridView标题文字
    PHP文件结尾符的问题
    JAVA中使用JSON进行数据传递
    Ubuntu LAMP环境安装
    PHP中使用class_exists判断类是否存在
    NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument
    Android开发教程 葵花宝典第六层 控件之 Dialog ListView GridView
  • 原文地址:https://www.cnblogs.com/keai/p/11633874.html
Copyright © 2020-2023  润新知