catamphetamine / react-phone-number-input

React component for international phone number input
http://catamphetamine.gitlab.io/react-phone-number-input/
MIT License
915 stars 193 forks source link

Expected string, received null #421

Closed enyelsequeira closed 1 year ago

enyelsequeira commented 1 year ago

Hello, first of all thank you for creating the package it has saved me a lot of time.

I have a question, is there a way to change this error? I am using react hook form with it and here is my code. I have a validation schema using zod and have tried to get around without any luck, any chance there a way to change that error?

  <PhoneInputWithCountry
                name="mobile"
                control={control}
                rules={{ required: true }}
                countrySelectComponent={PhoneCountry}
                // flagComponent={FlagComponent}
                international
                limitMaxLength
                placeholder="Enter phone number"
                defaultCountry="US"
                className="personal-input  flex justify-between text-[#E0DFE1]"
              />
mobile: z
    .string({
      description: 'telephone number',
      required_error: 'Telephone number is required',
    })
    .min(6, {
      message: 'Telephone number must be at least 6 characters',
    })
    .max(15, {
      message: 'Telephone number must be at most 15 characters',
    })
    .transform((value) => value.replace('+', ''))
    .refine((value) => value !== '', {
      message: 'Telephone number is required',
    }),

image

catamphetamine commented 1 year ago

Hi. I haven't used that validation library.

catamphetamine commented 1 year ago

If you're validating a value then it will be undefined when the input is empty.

enyelsequeira commented 1 year ago

is there any way to get around it? @catamphetamine

catamphetamine commented 1 year ago

No

catamphetamine commented 1 year ago

Or do something like value === undefined ? '' : value.