diff --git a/src/field/index.js b/src/field/index.js index 5d949beb8..cdbf0bff7 100644 --- a/src/field/index.js +++ b/src/field/index.js @@ -51,7 +51,6 @@ export default createComponent({ data() { return { focused: false, - validateError: false, validateMessage: '', }; }, @@ -59,6 +58,10 @@ export default createComponent({ watch: { value() { this.$nextTick(this.adjustSize); + + if (this.validateMessage) { + this.validateMessage = ''; + } }, }, @@ -133,7 +136,6 @@ export default createComponent({ return !this.rules.some(rule => { if (rule.required && !this.value) { - this.validateError = true; this.validateMessage = rule.message; return true; } @@ -395,7 +397,7 @@ export default createComponent({ titleClass={[bem('label', labelAlign), this.labelClass]} arrowDirection={this.arrowDirection} class={bem({ - error: this.error || this.validateError, + error: this.error || this.validateMessage, [`label-${labelAlign}`]: labelAlign, 'min-height': this.type === 'textarea' && !this.autosize, })} diff --git a/src/form/demo/index.vue b/src/form/demo/index.vue index 2834d9cb9..ff24d5d0d 100644 --- a/src/form/demo/index.vue +++ b/src/form/demo/index.vue @@ -42,7 +42,9 @@ export default { }, methods: { - onSubmit() {}, + onSubmit(values) { + console.log(values); + }, }, }; diff --git a/src/form/index.js b/src/form/index.js index f760be921..db6519f24 100644 --- a/src/form/index.js +++ b/src/form/index.js @@ -17,12 +17,28 @@ export default createComponent({ }, methods: { + // @exposed-api + validate() { + return this.fields.map(item => item.validate()).every(item => item); + }, + + getFormData() { + return this.fields.reduce((form, field) => { + form[field.name] = field.value; + return form; + }, {}); + }, + onSubmit(event) { event.preventDefault(); - const results = this.fields.map(item => item.validate()); + const valid = this.validate(); - console.log(results); + if (valid) { + this.$emit('submit', this.getFormData()); + } else { + this.$emit('failed'); + } }, },