Home > Blockchain >  Pass a field in required when a radio button value is clicked
Pass a field in required when a radio button value is clicked

Time:08-10

Basically I am using react hook form, I want to trigger a field to be required if I click a specific value in my radio button. Obviously it is dynamic so there could be anything in the radio button. I want to get the selected value from the radio and then depending on the selected value trigger another field to be required.

I have no clue on how to do it with react hook form; mayne someone has come across the same issue and have an example to show me

CodePudding user response:

you can use validate function from react hook form

<input
      {...register("test", {
        validate:{ required: value => getValues().radioBtn }
      })}
    />

CodePudding user response:

You can follow these steps:

  1. trigger a function once your desired radio button was clicked.

  2. get hew value How to get value from radio button

  3. set the attribute 'required' to true 'Required' attribute

let me know if that helps you

  • Related