Skip to content

Commit

Permalink
feat: added mandatory check presets for multiple selections
Browse files Browse the repository at this point in the history
  • Loading branch information
卫杰 committed Jan 13, 2025
1 parent eec4265 commit a91019a
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 1 deletion.
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 @@ -36,6 +36,12 @@ setupVbenForm<ComponentType>({
}
return true;
},
multipleRequired: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
return $t('ui.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 @@ -28,6 +28,12 @@ setupVbenForm<ComponentType>({
}
return true;
},
multipleRequired: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
return $t('ui.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 @@ -32,6 +32,12 @@ setupVbenForm<ComponentType>({
}
return true;
},
multipleRequired: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
return $t('ui.formRules.selectRequired', [ctx.label]);
}
return true;
},
},
});

Expand Down
6 changes: 6 additions & 0 deletions docs/src/_env/adapter/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ setupVbenForm<ComponentType>({
},
},
defineRules: {
multipleRequired: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
return $t('ui.formRules.selectRequired', [ctx.label]);
}
return true;
},
required: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
return $t('ui.formRules.required', [ctx.label]);
Expand Down
6 changes: 6 additions & 0 deletions docs/src/components/common-ui/vben-form.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ setupVbenForm<ComponentType>({
},
},
defineRules: {
multipleRequired: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
return $t('ui.formRules.selectRequired', [ctx.label]);
}
return true;
},
// 输入项目必填国际化适配
required: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
Expand Down
4 changes: 3 additions & 1 deletion packages/@core/ui-kit/form-ui/src/form-render/form-field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ const shouldRequired = computed(() => {
}
if (isString(currentRules.value)) {
return ['required', 'selectRequired'].includes(currentRules.value);
return ['multipleRequired', 'required', 'selectRequired'].includes(
currentRules.value,
);
}
let isOptional = currentRules?.value?.isOptional?.();
Expand Down
5 changes: 5 additions & 0 deletions packages/@core/ui-kit/form-ui/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ export interface VbenFormAdapterOptions<
modelPropNameMap?: Partial<Record<T, string>>;
};
defineRules?: {
multipleRequired?: (
value: any,
params: any,
ctx: Record<string, any>,
) => boolean | string;
required?: (
value: any,
params: any,
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 @@ -21,6 +21,12 @@ setupVbenForm<ComponentType>({
},
},
defineRules: {
multipleRequired: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
return $t('ui.formRules.selectRequired', [ctx.label]);
}
return true;
},
// 输入项目必填国际化适配
required: (value, _params, ctx) => {
if (value === undefined || value === null || value.length === 0) {
Expand Down

0 comments on commit a91019a

Please sign in to comment.