react-hook-form: how to show validaton message after getting response from api end point?

8.4k Views Asked by At

to validate an input filed in react-hook-form I am doing something like this

<input type="text" id="name" ref={register({ required: true, maxLength: 30 })} />

but thats at the submit time .. after I get response from the remote api endpoint I need to show some validation message .. in the same input field ...

1

There are 1 best solutions below

0
On BEST ANSWER

You can use trigger function to trigger validation programmatically:

const { trigger } = useForm();

const triggerValidation = () => {
 trigger() // all fields
 trigger("lastName") // specific field
}

Documentation: https://react-hook-form.com/api/#trigger