Commit 16f8b759bc8439d89cc1131253286074466f8277
1 parent
634c34e9
fix(FormItem): 解决required属性无法校验的bug
Showing
1 changed file
with
16 additions
and
8 deletions
Show diff stats
src/components/form/form-item.vue
... | ... | @@ -150,15 +150,19 @@ |
150 | 150 | methods: { |
151 | 151 | setRules() { |
152 | 152 | let rules = this.getRules(); |
153 | - if (rules.length) { | |
153 | + if (rules.length&&this.required) { | |
154 | + return; | |
155 | + }else if (rules.length) { | |
154 | 156 | rules.every((rule) => { |
155 | 157 | this.isRequired = rule.required; |
156 | 158 | }); |
157 | - this.$off('on-form-blur', this.onFieldBlur); | |
158 | - this.$off('on-form-change', this.onFieldChange); | |
159 | - this.$on('on-form-blur', this.onFieldBlur); | |
160 | - this.$on('on-form-change', this.onFieldChange); | |
159 | + }else if (this.required){ | |
160 | + this.isRequired = this.required; | |
161 | 161 | } |
162 | + this.$off('on-form-blur', this.onFieldBlur); | |
163 | + this.$off('on-form-change', this.onFieldChange); | |
164 | + this.$on('on-form-blur', this.onFieldBlur); | |
165 | + this.$on('on-form-change', this.onFieldChange); | |
162 | 166 | }, |
163 | 167 | getRules () { |
164 | 168 | let formRules = this.form.rules; |
... | ... | @@ -174,10 +178,14 @@ |
174 | 178 | return rules.filter(rule => !rule.trigger || rule.trigger.indexOf(trigger) !== -1); |
175 | 179 | }, |
176 | 180 | validate(trigger, callback = function () {}) { |
177 | - const rules = this.getFilteredRule(trigger); | |
181 | + let rules = this.getFilteredRule(trigger); | |
178 | 182 | if (!rules || rules.length === 0) { |
179 | - callback(); | |
180 | - return true; | |
183 | + if (!this.required) { | |
184 | + callback(); | |
185 | + return true; | |
186 | + }else { | |
187 | + rules = [{required: true}]; | |
188 | + } | |
181 | 189 | } |
182 | 190 | |
183 | 191 | this.validateState = 'validating'; | ... | ... |