form.ts 1.1 KB

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