How do you add validation to a reactive form?
Model answer
Give each control validators when you create it: new FormControl('', [Validators.required, Validators.email]). Built-in validators include required, minLength, maxLength, min, max, pattern and email. In the template, check the control's state, for example show the error only when it is invalid and touched.
You can also disable the submit button while the form is invalid, but always validate again on the server.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is the difference between template-driven and reactive forms?
- How do you write a custom validator?
- What are async validators and when do you need one?
- What are FormArray and dynamic forms?
- What is the difference between valueChanges and statusChanges, and how do you use them safely?
- What do touched, dirty and pristine mean?