fix: form validateMessages not work
parent
5ce268846b
commit
e66a35e1f2
|
@ -145,7 +145,12 @@ const Form = defineComponent({
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
const validateMessages = computed(() => {
|
||||||
|
return {
|
||||||
|
...defaultValidateMessages,
|
||||||
|
...props.validateMessages,
|
||||||
|
};
|
||||||
|
});
|
||||||
const formClassName = computed(() =>
|
const formClassName = computed(() =>
|
||||||
classNames(prefixCls.value, {
|
classNames(prefixCls.value, {
|
||||||
[`${prefixCls.value}-${props.layout}`]: true,
|
[`${prefixCls.value}-${props.layout}`]: true,
|
||||||
|
@ -267,10 +272,7 @@ const Form = defineComponent({
|
||||||
// Add field validate rule in to promise list
|
// Add field validate rule in to promise list
|
||||||
if (!provideNameList || containsNamePath(namePathList, fieldNamePath)) {
|
if (!provideNameList || containsNamePath(namePathList, fieldNamePath)) {
|
||||||
const promise = field.validateRules({
|
const promise = field.validateRules({
|
||||||
validateMessages: {
|
validateMessages: validateMessages.value,
|
||||||
...defaultValidateMessages,
|
|
||||||
...props.validateMessages,
|
|
||||||
},
|
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -376,6 +378,7 @@ const Form = defineComponent({
|
||||||
onValidate: (name, status, errors) => {
|
onValidate: (name, status, errors) => {
|
||||||
emit('validate', name, status, errors);
|
emit('validate', name, status, errors);
|
||||||
},
|
},
|
||||||
|
validateMessages,
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
|
|
|
@ -186,9 +186,20 @@ export default defineComponent({
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
validateState.value = props.validateStatus;
|
validateState.value = props.validateStatus;
|
||||||
});
|
});
|
||||||
|
const messageVariables = computed(() => {
|
||||||
|
let variables: Record<string, string> = {};
|
||||||
|
if (typeof props.label === 'string') {
|
||||||
|
variables.label = props.label;
|
||||||
|
} else if (props.name) {
|
||||||
|
variables.label = String(name);
|
||||||
|
}
|
||||||
|
if (props.messageVariables) {
|
||||||
|
variables = { ...variables, ...props.messageVariables };
|
||||||
|
}
|
||||||
|
return variables;
|
||||||
|
});
|
||||||
const validateRules = (options: ValidateOptions) => {
|
const validateRules = (options: ValidateOptions) => {
|
||||||
const { validateFirst = false, messageVariables } = props;
|
const { validateFirst = false } = props;
|
||||||
const { triggerName } = options || {};
|
const { triggerName } = options || {};
|
||||||
|
|
||||||
let filteredRules = rulesRef.value;
|
let filteredRules = rulesRef.value;
|
||||||
|
@ -209,9 +220,12 @@ export default defineComponent({
|
||||||
namePath.value,
|
namePath.value,
|
||||||
fieldValue.value,
|
fieldValue.value,
|
||||||
filteredRules as RuleObject[],
|
filteredRules as RuleObject[],
|
||||||
options,
|
{
|
||||||
|
validateMessages: formContext.validateMessages.value,
|
||||||
|
...options,
|
||||||
|
},
|
||||||
validateFirst,
|
validateFirst,
|
||||||
messageVariables,
|
messageVariables.value,
|
||||||
);
|
);
|
||||||
validateState.value = 'validating';
|
validateState.value = 'validating';
|
||||||
errors.value = [];
|
errors.value = [];
|
||||||
|
|
|
@ -3,7 +3,8 @@ import { inject, provide, computed } from 'vue';
|
||||||
import type { ColProps } from '../grid';
|
import type { ColProps } from '../grid';
|
||||||
import type { RequiredMark, ValidationRule } from './Form';
|
import type { RequiredMark, ValidationRule } from './Form';
|
||||||
import type { ValidateStatus, FieldExpose } from './FormItem';
|
import type { ValidateStatus, FieldExpose } from './FormItem';
|
||||||
import type { FormLabelAlign } from './interface';
|
import type { FormLabelAlign, ValidateMessages } from './interface';
|
||||||
|
import { defaultValidateMessages } from './utils/messages';
|
||||||
|
|
||||||
export interface FormContextProps {
|
export interface FormContextProps {
|
||||||
model?: ComputedRef<any>;
|
model?: ComputedRef<any>;
|
||||||
|
@ -24,6 +25,7 @@ export interface FormContextProps {
|
||||||
status: boolean,
|
status: boolean,
|
||||||
errors: string[] | null,
|
errors: string[] | null,
|
||||||
) => void;
|
) => void;
|
||||||
|
validateMessages: ComputedRef<ValidateMessages>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const FormContextKey: InjectionKey<FormContextProps> = Symbol('formContextKey');
|
export const FormContextKey: InjectionKey<FormContextProps> = Symbol('formContextKey');
|
||||||
|
@ -44,6 +46,7 @@ export const useInjectForm = () => {
|
||||||
rules: computed(() => undefined),
|
rules: computed(() => undefined),
|
||||||
requiredMark: computed(() => false),
|
requiredMark: computed(() => false),
|
||||||
onValidate: () => {},
|
onValidate: () => {},
|
||||||
|
validateMessages: computed(() => defaultValidateMessages),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue