Browse Source

fix: useForm support dynamic add rule #4498

close #4498 #4557
pull/4569/head
tangjinzhou 3 years ago
parent
commit
c6c004ce8d
  1. 35
      components/form/useForm.ts
  2. 2
      v2-doc

35
components/form/useForm.ts

@ -119,19 +119,33 @@ function useForm(
clearValidate: (names?: namesType) => void; clearValidate: (names?: namesType) => void;
} { } {
const initialModel = cloneDeep(unref(modelRef)); const initialModel = cloneDeep(unref(modelRef));
let validateInfos: validateInfos = {}; const validateInfos = reactive<validateInfos>({});
const rulesKeys = computed(() => { const rulesKeys = computed(() => {
return Object.keys(unref(rulesRef)); return Object.keys(unref(rulesRef));
}); });
rulesKeys.value.forEach(key => { watch(
validateInfos[key] = { rulesKeys,
autoLink: false, () => {
required: isRequired(unref(rulesRef)[key]), const newValidateInfos = {};
}; rulesKeys.value.forEach(key => {
}); newValidateInfos[key] = validateInfos[key] || {
validateInfos = reactive(validateInfos); autoLink: false,
required: isRequired(unref(rulesRef)[key]),
};
delete validateInfos[key];
});
for (const key in validateInfos) {
if (Object.prototype.hasOwnProperty.call(validateInfos, key)) {
delete validateInfos[key];
}
}
Object.assign(validateInfos, newValidateInfos);
},
{ immediate: true },
);
const resetFields = (newValues: Props) => { const resetFields = (newValues: Props) => {
Object.assign(unref(modelRef), { Object.assign(unref(modelRef), {
...cloneDeep(initialModel), ...cloneDeep(initialModel),
@ -251,6 +265,9 @@ function useForm(
}, },
!!option.validateFirst, !!option.validateFirst,
); );
if (!validateInfos[name]) {
return promise.catch((e: any) => e);
}
validateInfos[name].validateStatus = 'validating'; validateInfos[name].validateStatus = 'validating';
promise promise
.catch((e: any) => e) .catch((e: any) => e)
@ -327,7 +344,9 @@ function useForm(
validate(names, { trigger: 'change' }); validate(names, { trigger: 'change' });
oldModel = cloneDeep(model); oldModel = cloneDeep(model);
}; };
const debounceOptions = options?.debounce; const debounceOptions = options?.debounce;
watch( watch(
modelRef, modelRef,
debounceOptions && debounceOptions.wait debounceOptions && debounceOptions.wait

2
v2-doc

@ -1 +1 @@
Subproject commit 2e39bc81a84e2cfa90e8863e8f18d5a3d3ab1995 Subproject commit 3f94a02d16bac2377d71e26948ffe45571dae49e
Loading…
Cancel
Save