React Hook Form - useForm: setError

preview_player
Показать описание
This session cover setError API inside react hook form.

Рекомендации по теме
Комментарии
Автор

For anyone else coming here to find a way to use setError and clearError with validation in the onSubmit the way I got it working:
<Button text="Login" onPress={handleSubmit(onSubmit, onError)} /> //Add onError next to the onSubmit call.

//This function checks if the error that occured is the unregisterdInput error and if it is, clear the error and run the onSubmit function.
function onError(errors) {
if (errors.notRegisteredInput) {

handleSubmit(onSubmit)();
}
}

//My setError for context
setError("notRegisteredInput", { type: "custom", message: "Invalid username or password" });

You might need to modify it for any of your edge cases but I can confirm that this onError event will only run if you have setError manualy.

jizazim
Автор

What if we have some dynamic field created by useFieldArray, like address[1].timeAtAddress, and if we throw custom error on these dynamically created fields, how to stop handleSubmit to clear these errors, as they are cleared from formState.errors as soon has handleSubmit(handler) is help me on this

ishantgarg
Автор

Thanks for the great how to’s. Is there a possibility to specify a custom error like in your example without using typescript? I learned that you just can seterror without arguments, but then I can’t see a solution to clear one single specified error with clearerrors. Thanks in advance

mateuszpytlinski
Автор

Is there another video that differentiates between the custom errors (non-registered field) and the use of the 'root.' prefix in the error name? It's really not clear from the docs why you wouldn't just always use the use custom error mechanism.

JoePrivett-crnp
Автор

I have a question, If I want to show error message of required and minLength (if didn't input anything then show required error and if input only 1 char then show minLength error), Can I use setError? and how to use it?

bbpb
Автор

If we set an error that is not associated with any specific field, for example, an API call error, where should we call `clearErrors` to allow the user to re-submit?

guilleliss
Автор

Hi, thanks for the videos.
I have a requirement when there is a From data and To date.But these fields are dependent.Form Date can be minimum 6 months from today and maximum today.While To date can be maximum 30 days from From days. Also I have to show error message like "Maximum To date can be 30 days from To date".
Please help.

iamabhirupdatta
Автор

sorry, i need help, how can i get the error validation message from backend?

aigerimshuguralieva
Автор

Thanks for the great how to’s. Is there a possibility to specify a custom error like in your example without using typescript? I learned that you just can seterror without arguments, but then I can’t see a solution to clear one single specified error with clearerrors. Thanks in advance

mateuszpytlinski