feat(Form): add failed event params

This commit is contained in:
陈嘉涵 2020-02-10 15:54:03 +08:00
parent 494e303061
commit cb83240372
3 changed files with 51 additions and 18 deletions

View File

@ -135,19 +135,29 @@ export default createComponent({
}
},
// @exposed-api
validate() {
return new Promise(resolve => {
if (!this.rules) {
return true;
resolve();
}
return !this.rules.some(rule => {
const messages = [];
this.rules.forEach(rule => {
if (rule.required && !this.formValue) {
this.validateMessage = rule.message;
return true;
messages.push(rule.message);
}
});
return false;
if (messages.length) {
this.validateMessage = messages[0];
resolve({
name: this.name,
messages,
});
} else {
resolve();
}
});
},

View File

@ -19,10 +19,20 @@ export default createComponent({
methods: {
// @exposed-api
validate() {
return this.fields.map(item => item.validate()).every(item => item);
return new Promise((resolve, reject) => {
Promise.all(this.fields.map(item => item.validate())).then(errors => {
errors = errors.filter(item => item);
if (errors.length) {
reject(errors);
} else {
resolve();
}
});
});
},
getFormData() {
getValues() {
return this.fields.reduce((form, field) => {
form[field.name] = field.formValue;
return form;
@ -32,13 +42,18 @@ export default createComponent({
onSubmit(event) {
event.preventDefault();
const valid = this.validate();
const values = this.getValues();
if (valid) {
this.$emit('submit', this.getFormData());
} else {
this.$emit('failed');
}
this.validate()
.then(() => {
this.$emit('submit', values);
})
.catch(errors => {
this.$emit('failed', {
values,
errors,
});
});
},
},

View File

@ -126,6 +126,10 @@ module.exports = {
path: 'field',
title: 'Field 输入框',
},
{
path: 'form',
title: 'Form 表单',
},
{
path: 'number-keyboard',
title: 'NumberKeyboard 数字键盘',
@ -461,6 +465,10 @@ module.exports = {
path: 'field',
title: 'Field',
},
{
path: 'form',
title: 'Form',
},
{
path: 'number-keyboard',
title: 'NumberKeyboard',