The purpose of my question is to find out if it has to be complicated or maybe I'm doing something wrong.
It's quite easy to show how powerful the form validation is for the purpose of a tutorial. But when it comes to a real life application then things get complicated. I started with a simple registration form with
- username
- password
- repeated password
If I used tutorial-like approach then I'd use *ngIf to display all kind of errors that appear above the corresponding input. The problem is that it leads to terrible user experience:
- error are shown and hidden that makes inputs jumping
- if I have many validation conditions for password then user gets quite a long list of errors
- the form starts to look like one big warning and is not concise
- repeated password shouts that it's not the same as password although user hasn't typed anything yet
Moreover in order to stop displaying errors if the form is not touched one need to add code like this all over the place in templates
myForm.get('controlName').invalid && (myForm.get('controlName').dirty || myForm.get('controlName').touched)
what starts to resemble PHP spaghetti code.
What I want to say it that even for very simple form the logic starts to be very complicated if one wants to crate a nice user experience. And what I've finished with so far I a logic in component that scans errors for particular controls and returns just one error at a time.
What is your experience? Do you have some advice or an example of best practices?