DEV Community


Discussion on: A new way to validate Angular Forms

martinspire profile image
Martin Spierings • Edited

I normally just make a custom validator that subscribes to the input that is needed for it to be validated. I'm not going to subscribe to all form changes because thats an expensive call to make.

So when I want to show only 1 error in the case of password and password confirmation, I make sure that the second error doesn't trigger if the first one is already shown. So if password is empty, don't show password confirm error. This can be done in the HTML too, no need for the controller to do that. The password field already blocks submission so you only need to do that once.

And then my password + confirmation is just my password input component, so I don't need to repeat that if I use it in multiple locations. I hardly ever make forms with just inputs.

So when I create a form, in the fields I define, I already insert my validators because as far as I know thát is the Angular way