fix: sueForm support dynamic rule, close #4799
parent
4fc7c1666a
commit
7d67e0e7f2
|
@ -1,5 +1,5 @@
|
||||||
import type { Ref } from 'vue';
|
import type { Ref } from 'vue';
|
||||||
import { computed, reactive, watch, nextTick, unref } from 'vue';
|
import { reactive, watch, nextTick, unref, shallowRef } from 'vue';
|
||||||
import cloneDeep from 'lodash-es/cloneDeep';
|
import cloneDeep from 'lodash-es/cloneDeep';
|
||||||
import intersection from 'lodash-es/intersection';
|
import intersection from 'lodash-es/intersection';
|
||||||
import isEqual from 'lodash-es/isEqual';
|
import isEqual from 'lodash-es/isEqual';
|
||||||
|
@ -120,30 +120,7 @@ function useForm(
|
||||||
const initialModel = cloneDeep(unref(modelRef));
|
const initialModel = cloneDeep(unref(modelRef));
|
||||||
const validateInfos = reactive<validateInfos>({});
|
const validateInfos = reactive<validateInfos>({});
|
||||||
|
|
||||||
const rulesKeys = computed(() => {
|
const rulesKeys = shallowRef([]);
|
||||||
return rulesRef ? Object.keys(unref(rulesRef)) : [];
|
|
||||||
});
|
|
||||||
|
|
||||||
watch(
|
|
||||||
rulesKeys,
|
|
||||||
() => {
|
|
||||||
const newValidateInfos = {};
|
|
||||||
rulesKeys.value.forEach(key => {
|
|
||||||
newValidateInfos[key] = validateInfos[key] || {
|
|
||||||
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), {
|
||||||
|
@ -350,6 +327,39 @@ function useForm(
|
||||||
|
|
||||||
const debounceOptions = options?.debounce;
|
const debounceOptions = options?.debounce;
|
||||||
|
|
||||||
|
let first = true;
|
||||||
|
watch(
|
||||||
|
rulesRef,
|
||||||
|
() => {
|
||||||
|
rulesKeys.value = rulesRef ? Object.keys(unref(rulesRef)) : [];
|
||||||
|
if (!first && options && options.validateOnRuleChange) {
|
||||||
|
validate();
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
},
|
||||||
|
{ deep: true, immediate: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
watch(
|
||||||
|
rulesKeys,
|
||||||
|
() => {
|
||||||
|
const newValidateInfos = {};
|
||||||
|
rulesKeys.value.forEach(key => {
|
||||||
|
newValidateInfos[key] = Object.assign({}, validateInfos[key], {
|
||||||
|
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 },
|
||||||
|
);
|
||||||
watch(
|
watch(
|
||||||
modelRef,
|
modelRef,
|
||||||
debounceOptions && debounceOptions.wait
|
debounceOptions && debounceOptions.wait
|
||||||
|
@ -358,16 +368,6 @@ function useForm(
|
||||||
{ immediate: options && !!options.immediate, deep: true },
|
{ immediate: options && !!options.immediate, deep: true },
|
||||||
);
|
);
|
||||||
|
|
||||||
watch(
|
|
||||||
rulesRef,
|
|
||||||
() => {
|
|
||||||
if (options && options.validateOnRuleChange) {
|
|
||||||
validate();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ deep: true },
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
modelRef,
|
modelRef,
|
||||||
rulesRef,
|
rulesRef,
|
||||||
|
|
Loading…
Reference in New Issue