0
votes

I am using PhoneInput along with react hook form, I want to enable save button only if phone number is valid

Code:

    <form onSubmit={handleSubmit(onSubmitRequest)}>
      .....................other code..............
       <Controller
        as={
          <PhoneInput
            id="pNum"
            placeholder="Enter phone number"
            className={classes.phoneInput}
            inputRef={register({required: true})}
            isValid={(inputNumber, onlyCountries) => {
              return onlyCountries.some((country) => {
                return startsWith(inputNumber, country.dialCode) || startsWith(country.dialCode, inputNumber);
              });
            }}
          />
        }
        name="phoneNumber"
        control={control}
      />
     ........................other code...................
     <Button
        fullWidth
        type="submit"
        variant="contained"
        color={'primary'}
        className={classes.submitBtn}
        data-testid="customerFormButton"
        disabled={!formState.isValid}
      >
        Save
      </Button>
  </form>

Here I used PhoneInput as controller along with isValid for it. How can I disable Save button for invalid phone number input?

2

2 Answers

2
votes

How are you? I believe that your problem is because you are not configuring the rules for the controller.

You need to change your controller to something like this:

<Controller
        as={
          <PhoneInput
            id="pNum"
            placeholder="Enter phone number"
            className={classes.phoneInput}
            inputRef={register}
            isValid={(inputNumber, onlyCountries) => {
              return onlyCountries.some((country) => {
                return startsWith(inputNumber, country.dialCode) || startsWith(country.dialCode, inputNumber);
              });
            }}
          />
        }
        name="phoneNumber"
        control={control}
        rules= {{required: true}}
      />
0
votes

ref cannot be currently used on this element. react-phone-input-2.

Until its supported, you can provide a hidden input field which updates its value when the phone updates its value and put the ref on that Example:

import React, { FC, useCallback } from 'react';
import { useFormContext } from 'react-hook-form';

import PhoneInput from 'react-phone-input-2';
import 'react-phone-input-2/lib/style.css';

interface Props {
  handleChange: (name: string, val: string) => void;
  defaultValue: string;
  name: string;
}

const MyComponent: FC<Props> = ({ defaultValue, name, handleChange }) => {
  const { register, setValue, watch } = useFormContext(); // Note: needs <FormProvider> in parent for this to be acessible

  const nameHidden = `${name}Hidden`;
  const handleChangePhone = useCallback(
    (val: string) => {
      setValue(nameHidden, val, { shouldValidate: true });
      handleChange(name, val);
    },
    [handleChange]
  );
  return (
    <>
      <PhoneInput value={defaultValue as string} country="gb" onChange={handleChangePhone} />
      <input
        type="hidden"
        name={nameHidden}
        defaultValue={defaultValue}
        ref={register({
          // validate stuff here...
        })}
      />
    </>
  );
};

export default MyComponent;