halp: using hooks for form validation, losing state
I've been playing around with the hooks api and I went to work building out a form validation hook.
The goal here would be to be glue between the browser's constraint validation api, custom validation functions [which have access to the entire form state] and react-strap for making it look nice.
I've got a working prototype here: https://codesandbox.io/s/rllw0jyzx4?fontsize=14
There's really two things here -- the useForm hook that keeps track of all the form elements and does validations on submit, and a FormInput component that wraps custom browser validation logic.
I'm not entirely sure if I'm doing things the right or wrong way. I have a few concerns with what I've done here:
- the form field is pretty heavy in terms of what's in there -- there's a lot of logic around validating and interfacing with the browser apis. I suppose it has to go somewhere but it seems like a lot of stuff going on in there.
- I needed to use
contextto provideregisterandunregisterroutines that would let the form hook know about new entries... and also aget_form_dataroutine that would return the state of the form for validation functions. - there's some interesting timing issues with the onChange handler, and state fields not being available in the
useFormhook's get_form_data routine. I ended up doing auseEffectlistening tovalue, and invoke validation from there.... it works, but it needs to go after useuseEffectthat registers the FormInput.... seems a bit greasy.
While the whole thing seems to work pretty well -- it showing the browser's validation messages for things like min-length and required, and allow for custom async validation -- it has a bug that I'm not sure how to fix.
Once the form is submitted, I have a few state variables from another hook for async routines, loading, error that sort of thing. When the request is started, I get new state variables here and because my form hook is re-rendered, I end up losing the state of each of the form inputs and they go back to their initial blank state.
I'm curious if anyone has any insights, maybe better ways to architect this, or tips on how to persist the state so a re-render doesn't blow everything away.
9 comments · 100% upvoted · other discussions
event.preventDefault() in your form submission handler will prevent the page from reloading.
tswaters [OP] 1 points · Mar 2019
Thanks, it's in there, in `useForm`.... That is one thing that is working quite well already :)
JamesLaBrie 1 points · Mar 2019
Have you tried calling it in the onSubmit method you have defined as well? Form.js line 26
tswaters [OP] 1 points · Mar 2019
Does the form submit for you? For me it just shows "error" or "success"
JamesLaBrie 1 points · Mar 2019
What is the desired output?
tswaters [OP] 1 points · Mar 2019
Oh, I'm mostly looking for feedback, thoughts on how others might approach this. The bug is that after starting submission the state from the form is lost - really any re-render will cause the state to get lost.
CakeDay--Bot 0 points · Mar 2019
OwO, what's this? It's your 8th Cakeday tswaters! hug
JamesLaBrie 1 points · Mar 2019
Also, what is the desire for making validations async?
tswaters [OP] 1 points · Mar 2019
If you want to hit an endpoint to perform inline validations, i.e. for a register form, is this user name already taken?