form.ts 982 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. modelPropNameMap: {
  11. Upload: 'fileList',
  12. CheckboxGroup: 'model-value',
  13. },
  14. },
  15. defineRules: {
  16. required: (value, _params, ctx) => {
  17. if (value === undefined || value === null || value.length === 0) {
  18. return $t('ui.formRules.required', [ctx.label]);
  19. }
  20. return true;
  21. },
  22. selectRequired: (value, _params, ctx) => {
  23. if (value === undefined || value === null) {
  24. return $t('ui.formRules.selectRequired', [ctx.label]);
  25. }
  26. return true;
  27. },
  28. },
  29. });
  30. const useVbenForm = useForm<ComponentType>;
  31. export { useVbenForm, z };
  32. export type VbenFormSchema = FormSchema<ComponentType>;
  33. export type { VbenFormProps };