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

View File

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

View File

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

View File

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