Skip to content

Commit

Permalink
fix: form onValueChange validate time series problem (#1372)
Browse files Browse the repository at this point in the history
* fix: form onValueChange validate time series problem

* fix: form onValueChange validate time series problem
  • Loading branch information
rayhomie authored Jan 9, 2025
1 parent ab39e7b commit 3a7f981
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
22 changes: 20 additions & 2 deletions demo/pages/Form/FormBasic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,30 @@ Page({
onLoad() {
this.form = new Form({
initialValues: { user: { account: 'andy', phone: '10000' } },
rules: {
account: [
{
required: true,
message: '请输入账号',
},
() => ({
// 一定需要时异步函数,返回Promise对象
validator: async (_, value) => {
if (value.length !== 11) {
throw new Error('请输入正确的账号');
}
},
}),
],
},
});
this.form.onValueChange('user.account', (value, allValues) => {
console.log('onValueChange:', value, allValues);
const validates = this.form.getFieldsValidatorStatus();
console.log('onValueChange:', value, allValues, validates);
});
this.form.onValuesChange((value, allValues) => {
console.log('onValuesChange:', value, allValues);
const validates = this.form.getFieldsValidatorStatus();
console.log('onValuesChange:', value, allValues, validates);
});
/// #if WECHAT
if (this.formRefList) {
Expand Down
11 changes: 10 additions & 1 deletion src/Form/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,20 @@ class Field extends EventEmitter {
message,
validateTrigger
);
this.ref.on((trigger, value, extraInfo?: any) => {
this.ref.on(async (trigger, value, extraInfo?: any) => {
if (trigger === 'onChange') {
this.setValue(value);
this.touched = true;
// 触发校验,需要在 onValueChange 之前执行
await Promise.all(
this.validateTrigger.map((item) => {
if (item === trigger) {
return this.validate();
}
})
);
this.emit('valueChange', value);
return;
} else if (trigger === 'didUnmount') {
this.emit('didUnmount');
} else if (trigger === 'deriveDataFromProps') {
Expand Down

0 comments on commit 3a7f981

Please sign in to comment.