-
In my react app, I have two address forms on one page that have two save address functions which save the address in the database. There's also one submit button that submits both two fields and navigates to the next page (The plus button in the circle adds the address to the saved addresses list): What I want to do is to validate the form fields and I'm already doing that by calling two instances of the
I've then added the handleSubmit method of these two hooks to the onClick ( This does indeed validate the forms individually but the problem arises when I'm trying to submit the whole page with the SUBMIT button. I still want to be able to validate both of these two address fields when pressing the general SUBMIT button but I have no idea how I can validate these two instances with one Is there even any way this can be possible at all? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Why do you use 2 instances of |
Beta Was this translation helpful? Give feedback.
-
Thanks to @TalgatSaribayev 's comment on Stack Overflow for leading me to this solution. I didn't need to set any specific validation rules for the address field and in the end I separated the sender and receiver forms into two different pages. First, I've got to point out that instead of getting the input values of
When I saved the input values with ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// As @TalgatSaribayev pointed out, creating just one
The problem was that the errors wouldn't get updated (cleared) even when they had passed the validation. As if the invalid input wouldn't attach So I resolved to use the First I created a toggle state which changes the trigger state:
Then used the
I assume the way I triggered the input field with the
This was the only way I managed to validate separate input fields apart from the general validation that occurs with I welcome any more answers that might lead to a better solution. |
Beta Was this translation helpful? Give feedback.
-
I had same problem, where I had to show multiple independent forms on the same page to be opened based on some conditions, some as sub-forms. The problem was seen when we submit one form, another form in dom used to get submitted.
|
Beta Was this translation helpful? Give feedback.
Thanks to @TalgatSaribayev 's comment on Stack Overflow for leading me to this solution.
I didn't need to set any specific validation rules for the address field and in the end I separated the sender and receiver forms into two different pages.
First, I've got to point out that instead of getting the input values of
postalCode
andaddress
fields with thegetValues
API, I used theuseWatch
hook to get the most updated values.When I saved the input values with
getValues
in a variable, I got the previous state instead of the most recent one and the only way to solve that was calling