menu

React Hook Form

📋 Custom React hooks for form validation without the hassle

Channels
Team
Posts
Chat
Members
Info
Show previous messages

December 10, 2019 at 3:08pm
Thank you for replying very quick. There are not errors, but I don't want to reset the value of name, only the value of "other"
  • reply
  • like
My bad, Spectrum doesn't support multi-line by pressing "Enter"
Edited
  • reply
  • like
Please, see me my edited answer.
Edited
  • reply
  • like
Please, see me my edited answer.
Waiting for the edit :)
  • reply
  • like
I already edited it. You can refresh the page.
Edited
  • reply
  • like
In my example is not exactly like my code but I had to make it easy and sort, the subform shouldn't be aware of the mainForm, they are independent components
  • reply
  • like
the idea of the subform is to add elements to a list that belongs to the main form
  • reply
  • like
when you add it I want that subform to be cleared
  • reply
  • like
I will try with that reset, but I find it weird the second reset clear the first form
  • reply
  • like
It's not weird. That's how form works
  • reply
  • like
That would be hard in your case. Either you make your main form aware of the value of your sub-form or vice versa
  • reply
  • like
// main aware of sub form
function MainForm() {
const { register, handleSubmit, getValues. reset } = useForm();
const handleSubFormSubmit = data => {
// do you submission here
// then reset
const { name } = getValues({ nest: true });
const subForm = {
other: "",
};
reset({
name,
...subForm
})
};
return (
<form onSubmit={handleSubmit(console.log)}>
<input name="name" ref={register} />
<SubForm onSubmit={handleSubFormSubmit} />
</form>
);
}
  • reply
  • like
I don't think the lib has form-scope awareness feature.
  • reply
  • like
bad assumption on my side :)
  • reply
  • like
I will have to find a way because I have 30 fields or more in my form
  • reply
  • like
I just posted an example on how to achieve that :)
  • reply
  • like
doing that with 30 fields ...
  • reply
  • like
30 fields in subForm or in mainForm?
  • reply
  • like
also tried in that way and didn't work
  • reply
  • like
everything get reseted
  • reply
  • like
actually look like the library is form-scope aware because the getValue of the subform only returns the values of the subform and not the ones from the mainform
  • reply
  • like
thank anyway, I set the values to undefined in the subform which is only 6 fields
  • reply
  • like
for me was weird that getValues and triggerValidatin was form-scope but no the reset
  • reply
  • like
We can continue our discussion here
  • reply
  • like