Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/web-antd/src/adapter/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
});

Expand Down
6 changes: 6 additions & 0 deletions apps/web-ele/src/adapter/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
});

Expand Down
6 changes: 6 additions & 0 deletions apps/web-naive/src/adapter/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
});

Expand Down
6 changes: 6 additions & 0 deletions packages/@core/ui-kit/form-ui/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type CustomRenderType = (() => Component | string) | string;

export type FormSchemaRuleType =
| 'required'
| 'selectRequired'
| null
| (Record<never, never> & string)
| ZodTypeAny;
Expand Down Expand Up @@ -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;
};
}
3 changes: 2 additions & 1 deletion packages/locales/src/langs/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
}
},
"formRules": {
"required": "Please enter {0}"
"required": "Please enter {0}",
"selectRequired": "Please select {0}"
},
"widgets": {
"document": "Document",
Expand Down
3 changes: 2 additions & 1 deletion packages/locales/src/langs/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
}
},
"formRules": {
"required": "请输入{0}"
"required": "请输入{0}",
"selectRequired": "请选择{0}"
},
"widgets": {
"document": "文档",
Expand Down
6 changes: 6 additions & 0 deletions playground/src/adapter/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
});

Expand Down
17 changes: 12 additions & 5 deletions playground/src/views/examples/form/rules.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const [Form, formApi] = useVbenForm({
defaultValue: undefined,
fieldName: 'options',
label: '下拉选',
rules: 'required',
rules: 'selectRequired',
},
{
component: 'RadioGroup',
Expand All @@ -120,7 +120,7 @@ const [Form, formApi] = useVbenForm({
},
fieldName: 'radioGroup',
label: '单选组',
rules: 'required',
rules: 'selectRequired',
},
{
component: 'CheckboxGroup',
Expand All @@ -139,7 +139,7 @@ const [Form, formApi] = useVbenForm({
},
fieldName: 'checkboxGroup',
label: '多选组',
rules: 'required',
rules: 'selectRequired',
},
{
component: 'Checkbox',
Expand All @@ -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',
},
],
// 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
Expand Down