Skip to content

Commit

Permalink
fix(Form): add dynamicRequireds when update rules
Browse files Browse the repository at this point in the history
  • Loading branch information
vvpvvp committed Jun 19, 2019
1 parent 03fd66e commit ddb4829
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
65 changes: 65 additions & 0 deletions doc/components/demos/form/form7.vue
@@ -0,0 +1,65 @@
<template>
<Form
ref="testForm"
:showErrorTip="true"
:rules="validationRules"
:validOnChange="true"
:model="data">
<FormItem label="类型" prop="type" :required="true">
<Select v-model="data.type" autosize :datas="typeParams"></Select>
</FormItem>
<FormItem label="配置项1" prop="item1" key="item1" :required="true" v-if="data.type>=2">
<input
type="text"
v-model="data.item1"
placeholder="请输入配置项1"
/>
</FormItem>
<FormItem label="配置项2" prop="item2" key="item2" :required="true" v-if="data.type>=3">
<input
type="text"
v-model="data.item2"
placeholder="请输入配置项2"/>
</FormItem>
<FormItem label="配置项3" prop="item3" key="item3" :required="true" v-if="data.type>=4">
<input
type="text"
v-model="data.item3"
placeholder="请输入配置项3"/>
</FormItem>
<FormItem>
<Button color="primary" @click="updateRules">重置rules</Button>
<Button color="primary" @click="submit">提交</Button>
</FormItem>
</Form>
</template>
<script>
export default {
name: 'test',
data() {
return {
typeParams: { 1: '类型1', 2: '类型2', 3: '类型3', 4: '类型4' },
data: {
type: null,
item1: null,
item2: null,
item3: null
},
validationRules: {}
};
},
methods: {
updateRules() {
this.validationRules = {};
},
submit() {
let validResult = this.$refs.testForm.valid();
console.log(validResult);
if (validResult.result) {
this.$Message('验证成功');
}
}
}
};
</script>
12 changes: 8 additions & 4 deletions src/components/form/form.vue
Expand Up @@ -111,6 +111,9 @@ export default {
handler() {
if (this.validator) {
if (this.rules) this.validator.updateRule(this.rules);
this.dynamicRequireds.forEach(item => {
this.validator.setConfig(item, { required: true });
});
} else if (this.model && this.rules) {
this.validator = new Validator(this.rules);
}
Expand Down Expand Up @@ -186,9 +189,7 @@ export default {
}
this.initRequires();
if (!this.validator) return false;
this.$nextTick(() => {
this.validator.setConfig(prop, options);
});
this.validator.setConfig(prop, options);
},
updateErrorMessage(prop, label) {
let message = {
Expand All @@ -215,7 +216,10 @@ export default {
return message;
},
removeProp(prop) {
delete this.dynamicRequireds[prop];
let index = this.dynamicRequireds.indexOf(prop);
if (index > -1) {
this.dynamicRequireds.splice(index, 1);
}
this.setConfig(prop, { required: false });
},
renderMessage(returnResult) {
Expand Down

0 comments on commit ddb4829

Please sign in to comment.