Best way to show error messages for angular reactive forms, one formcontrol multiple validation errors?

前端 未结 5 1332
春和景丽
春和景丽 2021-02-06 08:56

I am showing reactive form error messages as per the suggested approach of angular angular form validation error example.

html code of showing error on the page

5条回答
  •  感情败类
    2021-02-06 09:47

    If it's a small form I usually just use lots of *ngIf; however, a custom validator directive as mentioned above might be useful if your application is almost entirely forms in need of validation.

    Take a look at the source, to see how the built-in validators are set up. https://github.com/angular/angular/blob/2.0.0-rc.3/modules/%40angular/common/src/forms-deprecated/directives/validators.ts#L104-L124

    Here's an example I dug up, but I think it's a little overkill for most use cases. Just write an *ngIf line in the template HTML, instead of a whole new @Attribute...

    https://scotch.io/tutorials/how-to-implement-a-custom-validator-directive-confirm-password-in-angular-2

提交回复
热议问题