feat(form): 新增 validateOnInit,默认关闭初始化时自动校验

属性面板仍开启初始化校验,避免打开普通表单时立即展示错误态。
This commit is contained in:
roymondchen 2026-07-28 16:45:42 +08:00
parent 2116408bd4
commit b7c04edcd5
5 changed files with 26 additions and 3 deletions

View File

@ -14,6 +14,7 @@
:config="config"
:type-match-valid="true"
:extend-state="extendState"
:validate-on-init="true"
@change="submit"
@error="errorHandler"
></MForm>

View File

@ -88,6 +88,13 @@ const props = withDefaults(
labelWidth?: string;
/** 是否开启类型匹配校验 */
typeMatchValid?: boolean;
/**
* 初始化`config` / `initValues` 就绪后是否立即执行一次表单校验
*
* - `false`默认不自动校验避免打开表单时就展示错误态
* - `true`初始化完成后在 `nextTick` 中调用 `validate()`
*/
validateOnInit?: boolean;
disabled?: boolean;
height?: string;
stepActive?: string | number;
@ -156,6 +163,7 @@ const props = withDefaults(
labelPosition: 'right',
keyProp: '__key',
useFieldTextInError: true,
validateOnInit: false,
},
);
@ -353,9 +361,11 @@ watch(
//
initialized.value = !props.isCompare;
nextTick(() => {
tMagicFormRef.value?.validate();
});
if (props.validateOnInit) {
nextTick(() => {
tMagicFormRef.value?.validate();
});
}
});
if (props.isCompare) {

View File

@ -16,6 +16,7 @@
:use-field-text-in-error="useFieldTextInError"
:extend-state="extendState"
:type-match-valid="typeMatchValid"
:validate-on-init="validateOnInit"
@change="changeHandler"
></Form>
<slot></slot>
@ -59,6 +60,8 @@ const props = withDefaults(
labelWidth?: string;
/** 是否开启类型匹配校验 */
typeMatchValid?: boolean;
/** 透传给内部 `MForm`,初始化完成后是否立即校验(默认 `false` */
validateOnInit?: boolean;
disabled?: boolean;
size?: 'small' | 'default' | 'large';
confirmText?: string;
@ -74,6 +77,7 @@ const props = withDefaults(
values: () => ({}),
confirmText: '确定',
useFieldTextInError: true,
validateOnInit: false,
},
);

View File

@ -33,6 +33,7 @@
:prevent-submit-default="preventSubmitDefault"
:use-field-text-in-error="useFieldTextInError"
:type-match-valid="typeMatchValid"
:validate-on-init="validateOnInit"
:extend-state="extendState"
:theme="effectiveTheme"
@change="changeHandler"
@ -87,6 +88,8 @@ const props = withDefaults(
labelWidth?: string;
/** 是否开启类型匹配校验 */
typeMatchValid?: boolean;
/** 透传给内部 `MForm`,初始化完成后是否立即校验(默认 `false` */
validateOnInit?: boolean;
fullscreen?: boolean;
disabled?: boolean;
title?: string;
@ -122,6 +125,7 @@ const props = withDefaults(
showClose: true,
showCancel: true,
useFieldTextInError: true,
validateOnInit: false,
},
);

View File

@ -30,6 +30,7 @@
:prevent-submit-default="preventSubmitDefault"
:use-field-text-in-error="useFieldTextInError"
:type-match-valid="typeMatchValid"
:validate-on-init="validateOnInit"
:extend-state="extendState"
:theme="effectiveTheme"
@change="changeHandler"
@ -78,6 +79,8 @@ const props = withDefaults(
labelWidth?: string;
/** 是否开启类型匹配校验 */
typeMatchValid?: boolean;
/** 透传给内部 `MForm`,初始化完成后是否立即校验(默认 `false` */
validateOnInit?: boolean;
disabled?: boolean;
closeOnPressEscape?: boolean;
title?: string;
@ -106,6 +109,7 @@ const props = withDefaults(
values: () => ({}),
confirmText: '确定',
useFieldTextInError: true,
validateOnInit: false,
},
);