Skip to content

Commit 7d67e0e

Browse files
committed
fix: sueForm support dynamic rule, close #4799
1 parent 4fc7c16 commit 7d67e0e

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

components/form/useForm.ts

+34-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Ref } from 'vue';
2-
import { computed, reactive, watch, nextTick, unref } from 'vue';
2+
import { reactive, watch, nextTick, unref, shallowRef } from 'vue';
33
import cloneDeep from 'lodash-es/cloneDeep';
44
import intersection from 'lodash-es/intersection';
55
import isEqual from 'lodash-es/isEqual';
@@ -120,30 +120,7 @@ function useForm(
120120
const initialModel = cloneDeep(unref(modelRef));
121121
const validateInfos = reactive<validateInfos>({});
122122

123-
const rulesKeys = computed(() => {
124-
return rulesRef ? Object.keys(unref(rulesRef)) : [];
125-
});
126-
127-
watch(
128-
rulesKeys,
129-
() => {
130-
const newValidateInfos = {};
131-
rulesKeys.value.forEach(key => {
132-
newValidateInfos[key] = validateInfos[key] || {
133-
autoLink: false,
134-
required: isRequired(unref(rulesRef)[key]),
135-
};
136-
delete validateInfos[key];
137-
});
138-
for (const key in validateInfos) {
139-
if (Object.prototype.hasOwnProperty.call(validateInfos, key)) {
140-
delete validateInfos[key];
141-
}
142-
}
143-
Object.assign(validateInfos, newValidateInfos);
144-
},
145-
{ immediate: true },
146-
);
123+
const rulesKeys = shallowRef([]);
147124

148125
const resetFields = (newValues: Props) => {
149126
Object.assign(unref(modelRef), {
@@ -350,22 +327,45 @@ function useForm(
350327

351328
const debounceOptions = options?.debounce;
352329

330+
let first = true;
353331
watch(
354-
modelRef,
355-
debounceOptions && debounceOptions.wait
356-
? debounce(modelFn, debounceOptions.wait, omit(debounceOptions, ['wait']))
357-
: modelFn,
358-
{ immediate: options && !!options.immediate, deep: true },
332+
rulesRef,
333+
() => {
334+
rulesKeys.value = rulesRef ? Object.keys(unref(rulesRef)) : [];
335+
if (!first && options && options.validateOnRuleChange) {
336+
validate();
337+
}
338+
first = false;
339+
},
340+
{ deep: true, immediate: true },
359341
);
360342

361343
watch(
362-
rulesRef,
344+
rulesKeys,
363345
() => {
364-
if (options && options.validateOnRuleChange) {
365-
validate();
346+
const newValidateInfos = {};
347+
rulesKeys.value.forEach(key => {
348+
newValidateInfos[key] = Object.assign({}, validateInfos[key], {
349+
autoLink: false,
350+
required: isRequired(unref(rulesRef)[key]),
351+
});
352+
delete validateInfos[key];
353+
});
354+
for (const key in validateInfos) {
355+
if (Object.prototype.hasOwnProperty.call(validateInfos, key)) {
356+
delete validateInfos[key];
357+
}
366358
}
359+
Object.assign(validateInfos, newValidateInfos);
367360
},
368-
{ deep: true },
361+
{ immediate: true },
362+
);
363+
watch(
364+
modelRef,
365+
debounceOptions && debounceOptions.wait
366+
? debounce(modelFn, debounceOptions.wait, omit(debounceOptions, ['wait']))
367+
: modelFn,
368+
{ immediate: options && !!options.immediate, deep: true },
369369
);
370370

371371
return {

0 commit comments

Comments
 (0)