This commit is contained in:
梁灏 2017-04-25 14:50:37 +08:00
parent c5625bfdba
commit 05e2dda017
2 changed files with 27 additions and 66 deletions

View file

@ -1,78 +1,32 @@
<template>
<div style="width: 300px;">
<Form ref="formCustom" :model="formCustom" :rules="ruleCustom" :label-width="80">
<Form-item label="密码" prop="passwd">
<Input type="password" v-model="formCustom.passwd"></Input>
</Form-item>
<Form-item label="确认密码" prop="passwdCheck">
<Input type="password" v-model="formCustom.passwdCheck"></Input>
</Form-item>
<Form-item label="年龄" prop="age">
<Input type="text" v-model="formCustom.age" number></Input>
<i-form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
<Form-item label="爱好" prop="interest">
<Checkbox-group v-model="formValidate.interest">
<Checkbox label="吃饭"></Checkbox>
<Checkbox label="睡觉"></Checkbox>
<Checkbox label="跑步"></Checkbox>
<Checkbox label="看电影"></Checkbox>
</Checkbox-group>
</Form-item>
<Form-item>
<Button type="primary" @click="handleSubmit('formCustom')">提交</Button>
<Button type="ghost" @click="handleReset('formCustom')" style="margin-left: 8px">重置</Button>
<i-button type="primary" @click="handleSubmit('formValidate')">提交</i-button>
<i-button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</i-button>
</Form-item>
</Form>
</i-form>
</div>
</template>
<script>
export default {
data () {
const validatePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入密码'));
} else {
if (this.formCustom.passwdCheck !== '') {
//
this.$refs.formCustom.validateField('passwdCheck');
}
callback();
}
};
const validatePassCheck = (rule, value, callback) => {
if (value === '') {
callback(new Error('请再次输入密码'));
} else if (value !== this.formCustom.passwd) {
callback(new Error('两次输入密码不一致!'));
} else {
callback();
}
};
const validateAge = (rule, value, callback) => {
if (!value) {
return callback(new Error('年龄不能为空'));
}
//
setTimeout(() => {
if (!Number.isInteger(value)) {
callback(new Error('请输入数字值'));
} else {
if (value < 18) {
callback(new Error('必须年满18岁'));
} else {
callback();
}
}
}, 2000);
};
return {
formCustom: {
passwd: '',
passwdCheck: '',
age: ''
formValidate: {
interest: ['吃饭', '跑步']
},
ruleCustom: {
passwd: [
{ validator: validatePass, trigger: 'blur' }
],
passwdCheck: [
{ validator: validatePassCheck, trigger: 'blur' }
],
age: [
{ validator: validateAge, trigger: 'blur' }
ruleValidate: {
interest: [
{ required: true, type: 'array', min: 1, message: '至少选择一个爱好', trigger: 'change' },
{ type: 'array', max: 2, message: '最多选择两个爱好', trigger: 'change' }
]
}
}

View file

@ -191,10 +191,17 @@
let prop = getPropByPath(model, path);
if (Array.isArray(value) && value.length > 0) {
// if (Array.isArray(value) && value.length > 0) {
// this.validateDisabled = true;
// prop.o[prop.k] = [];
// } else if (value !== this.initialValue) {
// this.validateDisabled = true;
// prop.o[prop.k] = this.initialValue;
// }
if (Array.isArray(value)) {
this.validateDisabled = true;
prop.o[prop.k] = [];
} else if (value !== this.initialValue) {
prop.o[prop.k] = [].concat(this.initialValue);
} else {
this.validateDisabled = true;
prop.o[prop.k] = this.initialValue;
}