Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touch all fields on submit OR validate only fields that got blurred #29

Open
1 task done
christianlmc opened this issue Jul 4, 2023 · 11 comments · May be fixed by #31
Open
1 task done

Touch all fields on submit OR validate only fields that got blurred #29

christianlmc opened this issue Jul 4, 2023 · 11 comments · May be fixed by #31
Labels
enhancement New feature or request

Comments

@christianlmc
Copy link

christianlmc commented Jul 4, 2023

Describe the feature

Right now, when you set validateMode: 'blur' on useForm() ALL fields from the form get validated on blur.

So this is what I did in order to fix it:

<span v-show="touched.email">{{ errors.email }}</span>

However, by doing this, when you submit the form no message shows up, because none of the fields have been touched.

Ideally I would like to either have blur only trigger validation on the blurred field OR make form submission touch all fields

Additional information

  • Would you be willing to help implement this feature?
@lttr
Copy link

lttr commented Jul 27, 2023

It would be great to follow some authority like: https://www.nngroup.com/articles/errors-forms-design-guidelines/, maybe even make it the default.

In scope of this issue specifically it should be possible to provide the user with an error message right when she finishes typing. When she is correcting her mistake, it might provide feedback right away. Validation of other fields should not be triggered during her work on this specific field unless she hits enter or clicks on submit button.

@Mini-ghost
Copy link
Owner

I can't believe I missed this!

The following stackblitz example might be a temporary solution (but it's not that elegant)
https://stackblitz.com/edit/vormscore-issues-29?file=src%2FApp.vue&terminal=dev

I'm exploring the possibility of adding this functionality. One idea might be to trigger full validation whenever a field is blurred, and only keep error messages for fields that were ever touched.

Feel free to open a PR to implement this feature if you like. Thank you so much!

@Mini-ghost
Copy link
Owner

Mini-ghost commented Jul 27, 2023

@lttr Thank you so much for this article, it made a lot of great points, it was super useful for me.

@lttr
Copy link

lttr commented Jul 28, 2023

Well, I thank you for creating this library in the first place! 😄 I'll give it a try.

@lttr
Copy link

lttr commented Jul 28, 2023

Unfortunately the workaround you have posted @Mini-ghost does not really work. The problem is as the OP describes - it does not show the error message on untouched fields. Removing the condition for touched works better, however it is then probably the same behavior as the currently default validateMode: 'submit'.

I would add reValidateMode: 'input' which then behaves similar to what I have described before. And with the condition for touched removed, the example works quite well - it is a common behavior around the web.

I have however encountered another issue - if I want to validate a checkbox (imagine a checkbox field "You have to agree with terms and conditions") it does not behave correctly probably because checkbox and other elements fires change event instead of input event on an "atomic" user action.

In case we would use your library in production I would dive deeper, however I'm not able to work on the PR right now. I like the simplicity and the examples of smart components in the docs (over what Vuelidate or VeeValidate offer).

@Mini-ghost
Copy link
Owner

@lttr

Thank you so much for your awesome support and praise for Vorms!

I found that the workaround I provided had an issue with the condition between touched and submitCount, but I have fixed it. Could you please take a quick look and let me know what you think?

However, this solution is not entirely in line with the idea you previously provided, as it still requires ensuring the entire form is correct before submission, not just the fields that have been touched.

Your suggestions opened up new possibilities for me, and I'm excited to explore how to implement them. It might take a little while, but I'm super grateful for your help and guidance!

Once again, thank you for your support for Vorms and your assistance. You rock!

@christianlmc
Copy link
Author

@Mini-ghost Yeah, the workaround you provided is the desired behavior that I have on my project, I did things a little different, but the result is the same.

I'll try to work on a PR for this weekend. Thanks for considering my suggestions

@lttr
Copy link

lttr commented Jul 31, 2023

I found that the workaround I provided had an issue with the condition between touched and submitCount, but I have fixed it. Could you please take a quick look and let me know what you think?

@Mini-ghost It seems to work!

If you guys would be willing to implement the default behavior close to the recommendations, it would be absolutely great. The boilerplate code is problematic, since developers would not get it right usually, unless it is well encapsulated in a library code.

@christianlmc
Copy link
Author

Update: I got the general functionality working as intended locally. I'll just polish the code, add some tests and open a PR soon​™

@christianlmc
Copy link
Author

christianlmc commented Aug 1, 2023

@Mini-ghost just noticed the same issue is happening when we use validateMode: 'input' and validateMode: 'change'

I'll update the PR

@christianlmc
Copy link
Author

christianlmc commented Aug 1, 2023

@Mini-ghost Actually, input and change do not update the touched object. dirty property from useForm is a boolean that returns true if the form is dirty. Should we change that behavior as well? Ideally we would want a dirty object in order to implement the same behavior for input and change

@Mini-ghost Mini-ghost added the enhancement New feature or request label Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants