Skip to content

Commit

Permalink
feat: default form validation rules applicable to selector components (
Browse files Browse the repository at this point in the history
…#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

---------
  • Loading branch information
imdap authored Sep 20, 2024
1 parent a634ec3 commit fbd2370
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 7 deletions.
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

0 comments on commit fbd2370

Please sign in to comment.