form.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. // naive-ui组件的空值为null,不能是undefined,否则重置表单时不生效
  11. emptyStateValue: null,
  12. baseModelPropName: 'value',
  13. modelPropNameMap: {
  14. Checkbox: 'checked',
  15. Radio: 'checked',
  16. Upload: 'fileList',
  17. },
  18. },
  19. defineRules: {
  20. required: (value, _params, ctx) => {
  21. if (value === undefined || value === null || value.length === 0) {
  22. return $t('ui.formRules.required', [ctx.label]);
  23. }
  24. return true;
  25. },
  26. selectRequired: (value, _params, ctx) => {
  27. if (value === undefined || value === null) {
  28. return $t('ui.formRules.selectRequired', [ctx.label]);
  29. }
  30. return true;
  31. },
  32. },
  33. });
  34. const useVbenForm = useForm<ComponentType>;
  35. export { useVbenForm, z };
  36. export type VbenFormSchema = FormSchema<ComponentType>;
  37. export type { VbenFormProps };