form.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import type {
  2. VbenFormSchema as FormSchema,
  3. VbenFormProps,
  4. } from '@vben/common-ui';
  5. import type { ComponentType } from './component';
  6. import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
  7. import { $t } from '@vben/locales';
  8. setupVbenForm<ComponentType>({
  9. config: {
  10. // ant design vue组件库默认都是 v-model:value
  11. baseModelPropName: 'value',
  12. // 一些组件是 v-model:checked 或者 v-model:fileList
  13. modelPropNameMap: {
  14. Checkbox: 'checked',
  15. Radio: 'checked',
  16. Switch: 'checked',
  17. Upload: 'fileList',
  18. },
  19. },
  20. defineRules: {
  21. // 输入项目必填国际化适配
  22. required: (value, _params, ctx) => {
  23. if (value === undefined || value === null || value.length === 0) {
  24. return $t('formRules.required', [ctx.label]);
  25. }
  26. return true;
  27. },
  28. // 选择项目必填国际化适配
  29. selectRequired: (value, _params, ctx) => {
  30. if (value === undefined || value === null) {
  31. return $t('formRules.selectRequired', [ctx.label]);
  32. }
  33. return true;
  34. },
  35. },
  36. });
  37. const useVbenForm = useForm<ComponentType>;
  38. export { useVbenForm, z };
  39. export type VbenFormSchema = FormSchema<ComponentType>;
  40. export type { VbenFormProps };