user944513
user944513

Reputation: 12729

remove required validation on input field (if I entered some text in another input field)

remove required validation on input field (if I entered some text in another input field) .I created input fields (two input field )from json .Initially both are required.But if user enter "hello" in first field I want to remove required check from second input field.I used watch and onchange I checked the value of first field .but how to remove required ?I tried by using unregister but still not working

const onChange = e => {
    console.log(e.target.name);
    if (e.target.name == "agencyName") {
      if (agencyName == "hello") {
        //remove required
        unregister({ required: false, name: "contactPerson" });
      }
    }
  };

here is my code https://codesandbox.io/s/react-hook-form-watch-unir2

API link

https://react-hook-form.com/api/

Upvotes: 0

Views: 1480

Answers (1)

Paul
Paul

Reputation: 1022

Here's the updated script which makes the Contact Person optional when Agency Name is equal to "hello".

https://codesandbox.io/s/react-hook-form-watch-j396u

Upvotes: 2

Related Questions