BearStudio/formiz

Field errors show up after form reset

trompx opened this issue · 5 comments

Is your feature request related to a problem? Please describe.
Not sure if this is a bug but when I submit my form, if it succeeds, I reset the form. In that case, all the fields errors show up as isSubmitted and isTouched are true and the fields are empty.

Describe the solution you'd like
Resetting the form should also reset the fields to their initial state.

Hello @trompx , it seems it's a bug.
Do you have some code to reproduce this issue?

Hello @ivan-dalmet,
Sorry to get back this late. I haven't directly a repo I can share, but what does reset function exactly do?

If it just resets the fields to value = "", it is normal the error show up as in my form field components, I have const showError = !isValid && (isTouched || isSubmitted);

My guess is that isTouched and isSubmitted do not reset to false. Can you confirm?

My bad. When creating my form field component, I forgot to add:

useEffect(() => {
    setIsTouched(false);
  }, [resetKey]);

for the input and textarea^^ All is working perfectly now ! Thanks again :)

@trompx the isTouched is not coming from Formiz so you should take car of it yourself.
isSubmitted should be reinitialised to false.
You can look at the example here https://formiz-examples.netlify.app/
Look at the FieldInput component implementation https://github.com/ivan-dalmet/formiz/blob/master/examples/src/components/Fields/FieldInput.tsx
The reset of isTouched is handle like that:

useEffect(() => { 
   setIsTouched(false); 
}, [resetKey]);

Oh you found the solution yourself nice ☺️ (I didn't saw your last message)