2

I am new to yup validation, and I am populating the react-select dropdown with the below options. and now when I click on one button trying to validate if any value is selected or not. but it is not validating. any help is much appreciated.

const options = [
        { value: 'active', label: 'Active' },
        { value: 'inactive', label: 'In Active' },
        { value: 'deleted', label: 'Delete' },
 ];

<Select
  defaultValue={options[0]}
  isSearchable={false}
  className="react-dropdown"
  onChange={statusDropdownHandleChange}
  classNamePrefix="dropdown"
  options={options}
  name="status"
  {...register("status")}
/>


let schema = yup.object().shape({
    status: yup.object().shape({
      label: yup.string().required("status is required"),
      value: yup.string().required("status is required")
    })
 });

1 Answer 1

11

The validation should be working but if you are using Select with react-hook-form directly, you will hit the error/value undefined when choosing value/submitting form as Select doesn't expose input's ref. Therefore, you need to wrap Select with Controller to register the component.

For validating the form, there is one more case to handle if you allow isClearable in Select where the value will be null instead of {label: undefined, value: undefined} so there is a need to add .nullable() and .required() at the end of status's validation.

validation

const schema = yup.object().shape({
  status: yup
    .object()
    .shape({
      label: yup.string().required("status is required (from label)"),
      value: yup.string().required("status is required")
    })
    .nullable() // for handling null value when clearing options via clicking "x"
    .required("status is required (from outter null check)")
});

form with react-select

<form onSubmit={handleSubmit(onSubmit)}>
    <Controller
        name="status"
        control={control}
        render={({ field }) => (
        <Select
            // defaultValue={options[0]}
            {...field}
            isClearable
            isSearchable={false}
            className="react-dropdown"
            classNamePrefix="dropdown"
            options={options}
        />
        )}
    />
    <p>{errors.status?.message || errors.status?.label.message}</p>
    <input type="submit" />
</form>

Here is the codesandbox

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot, Mic. I spent a lot of time on it.
I had never valued a StackOverflow response this much. There is a very vague description on the react-hook-form official site ( link ) "... If the component doesn't expose input's ref, ..." and your response to this question made it all very clear to me. I had spent many hours in this. Thank you for your kindness. God bless you!
and if select is multipe ? will it be array instead of object
is there a way to return only the value?
Are you sure react-select doesn't expose input ref?. You can use both register or Controller.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.