feat: default form validation rules applicable to selector components (#4442)

* fix: hover border style same as antd style when validate error

* fix: hover border style same as antd style when validate error

* feat(@vben-core/form-ui): Default form validation rules applicable to selector components

---------
pull/48/MERGE
LinaBell 2024-09-20 09:46:03 +08:00 committed by GitHub
parent a634ec3692
commit fbd23701de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 46 additions and 7 deletions

View File

@ -103,6 +103,12 @@ setupVbenForm<FormComponentType>({
} }
return true; return true;
}, },
selectRequired: (value, _params, ctx) => {
if (value === undefined || value === null) {
return $t('formRules.selectRequired', [ctx.label]);
}
return true;
},
}, },
}); });

View File

@ -78,6 +78,12 @@ setupVbenForm<FormComponentType>({
} }
return true; return true;
}, },
selectRequired: (value, _params, ctx) => {
if (value === undefined || value === null) {
return $t('formRules.selectRequired', [ctx.label]);
}
return true;
},
}, },
}); });

View File

@ -87,6 +87,12 @@ setupVbenForm<FormComponentType>({
} }
return true; return true;
}, },
selectRequired: (value, _params, ctx) => {
if (value === undefined || value === null) {
return $t('formRules.selectRequired', [ctx.label]);
}
return true;
},
}, },
}); });

View File

@ -58,6 +58,7 @@ export type CustomRenderType = (() => Component | string) | string;
export type FormSchemaRuleType = export type FormSchemaRuleType =
| 'required' | 'required'
| 'selectRequired'
| null | null
| (Record<never, never> & string) | (Record<never, never> & string)
| ZodTypeAny; | ZodTypeAny;
@ -323,5 +324,10 @@ export interface VbenFormAdapterOptions<
params: any, params: any,
ctx: Record<string, any>, ctx: Record<string, any>,
) => boolean | string; ) => boolean | string;
selectRequired?: (
value: any,
params: any,
ctx: Record<string, any>,
) => boolean | string;
}; };
} }

View File

@ -55,7 +55,8 @@
} }
}, },
"formRules": { "formRules": {
"required": "Please enter {0}" "required": "Please enter {0}",
"selectRequired": "Please select {0}"
}, },
"widgets": { "widgets": {
"document": "Document", "document": "Document",

View File

@ -55,7 +55,8 @@
} }
}, },
"formRules": { "formRules": {
"required": "请输入{0}" "required": "请输入{0}",
"selectRequired": "请选择{0}"
}, },
"widgets": { "widgets": {
"document": "文档", "document": "文档",

View File

@ -103,6 +103,12 @@ setupVbenForm<FormComponentType>({
} }
return true; return true;
}, },
selectRequired: (value, _params, ctx) => {
if (value === undefined || value === null) {
return $t('formRules.selectRequired', [ctx.label]);
}
return true;
},
}, },
}); });

View File

@ -102,7 +102,7 @@ const [Form, formApi] = useVbenForm({
defaultValue: undefined, defaultValue: undefined,
fieldName: 'options', fieldName: 'options',
label: '下拉选', label: '下拉选',
rules: 'required', rules: 'selectRequired',
}, },
{ {
component: 'RadioGroup', component: 'RadioGroup',
@ -120,7 +120,7 @@ const [Form, formApi] = useVbenForm({
}, },
fieldName: 'radioGroup', fieldName: 'radioGroup',
label: '单选组', label: '单选组',
rules: 'required', rules: 'selectRequired',
}, },
{ {
component: 'CheckboxGroup', component: 'CheckboxGroup',
@ -139,7 +139,7 @@ const [Form, formApi] = useVbenForm({
}, },
fieldName: 'checkboxGroup', fieldName: 'checkboxGroup',
label: '多选组', label: '多选组',
rules: 'required', rules: 'selectRequired',
}, },
{ {
component: 'Checkbox', component: 'Checkbox',
@ -150,14 +150,21 @@ const [Form, formApi] = useVbenForm({
default: () => ['我已阅读并同意'], default: () => ['我已阅读并同意'],
}; };
}, },
rules: 'required', rules: 'selectRequired',
}, },
{ {
component: 'DatePicker', component: 'DatePicker',
defaultValue: undefined, defaultValue: undefined,
fieldName: 'datePicker', fieldName: 'datePicker',
label: '日期选择框', label: '日期选择框',
rules: 'required', rules: 'selectRequired',
},
{
component: 'RangePicker',
defaultValue: undefined,
fieldName: 'rangePicker',
label: '区间选择框',
rules: 'selectRequired',
}, },
], ],
// 321 // 321