You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-get-validators.ts 961B

123456789101112131415161718192021222324252627282930313233343536
  1. import { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import type { FormSchema } from '../types'
  4. export const useGetValidators = () => {
  5. const { t } = useTranslation()
  6. const getValidators = useCallback((formSchema: FormSchema) => {
  7. const {
  8. name,
  9. validators,
  10. required,
  11. } = formSchema
  12. let mergedValidators = validators
  13. if (required && !validators) {
  14. mergedValidators = {
  15. onMount: ({ value }: any) => {
  16. if (!value)
  17. return t('common.errorMsg.fieldRequired', { field: name })
  18. },
  19. onChange: ({ value }: any) => {
  20. if (!value)
  21. return t('common.errorMsg.fieldRequired', { field: name })
  22. },
  23. onBlur: ({ value }: any) => {
  24. if (!value)
  25. return t('common.errorMsg.fieldRequired', { field: name })
  26. },
  27. }
  28. }
  29. return mergedValidators
  30. }, [t])
  31. return {
  32. getValidators,
  33. }
  34. }