mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(Form): add submit、failed event
This commit is contained in:
parent
9d8cf7f8d3
commit
70ed34aa24
@ -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,
|
||||
})}
|
||||
|
@ -42,7 +42,9 @@ export default {
|
||||
},
|
||||
|
||||
methods: {
|
||||
onSubmit() {},
|
||||
onSubmit(values) {
|
||||
console.log(values);
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
@ -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');
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user