react-use-form-state icon indicating copy to clipboard operation
react-use-form-state copied to clipboard

Question - trigger validation on submit

Open IrvingArmenta opened this issue 3 years ago • 1 comments

Is there a way to trigger validation for all inputs on "Submit" ? something like formState.validateFields() or something.

I don't want to use the HTML5 api for form validation at all (<form noValidate={true} />) , therefore when the user clicks "submit" I want the fields that are required to show the error message that they're required. This is how react-hook-formand other frameworks work.

I understand I can "disable" the submit button with the Pristine() function, but I don't want to disable this button, also, if there is a field input that needs to have the same value as other input (password confirmation or email confirmation) the user can just click "submit" either way, since Pristine() just throws false at this point.

Is there a work around for this functionality?

IrvingArmenta avatar Oct 07 '20 05:10 IrvingArmenta

A possible workaround would be to validate each field by inspecting formState.values. For each invalid field, you could call formState.setFieldError. Although this requires a bit of extra code, I think this would get you your desired result.

wouterraateland avatar Dec 22 '20 11:12 wouterraateland