<el-form ref="form" :model="userInfo" label-width="100px" :rules="rules"> <el-row> <el-col :span="12"> <el-form-item label="登陆账号:" prop="loginId"> <el-input v-model="userInfo.loginId" placeholder="请输入"></el-input> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="12"> <el-form-item label="姓名:" prop="username"> <el-input v-model="userInfo.username" placeholder="请输入"></el-input> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="12"> <el-form-item label="状态:" prop="enabled"> <el-select v-model="userInfo.enabled" style=" 100%;"> <el-option v-for="item in stateList" :key="item.value" :label="item.label" :value="item.value" ></el-option> </el-select> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="12"> <el-form-item label="手机号:" prop="mobile"> <el-input v-model="userInfo.mobile" placeholder="请输入"></el-input> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="12"> <el-form-item label="邮箱:" prop="email"> <el-input v-model="userInfo.email" placeholder="请输入"></el-input> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="12"> <div class="btn-group"> <el-button type="primary" @click="validateForm('form')">保存</el-button> </div> </el-col> </el-row> </el-form>
let validatePhone = (rule, value, callback) => {
if (value&&value.trim()&&!/^1[34578]d{9}$/.test(value)) {
callback(new Error("请输入正确的手机号"));
} else {
callback();
}
};
let validateEmail = (rule, value, callback) => {
if (value&&value.trim()&&!/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/.test(value)) {
callback(new Error("请输入正确的邮箱号"));
} else {
callback();
}
};
export default {
data() {
return {
userInfo: {
loginId:'',
username:'',
mobile:'',
email:'',
},
rules: {
loginId: [{ required: true, message: "请输入登录账号" }],
username: [{ required: true, message: "请输入用户名" }],
mobile: [{ validator: validatePhone }],
email:[{ validator: validateEmail }],
enabled:[{ required: true, message: "请选择状态" }],
}
};
},
methods: {
validateForm(formName) {
this.$refs[formName].validate(async valid => {
if (valid) {
//后台提交
await this.save();
} else {
return false;
}
});
},
}
};