Method 1</button> <button (click)="clearInputMethod2 ()">Clear Input Method 2</button> `, }) export class AppComponent { form = new FormGroup ( { first: new FormControl ('Nancy', Validators.minLength (2) ), last: new FormControl ('Drew'), }); Compiling application & starting dev server formgroup-clear-input.stackblitz.io Console Clear on reload The first step is to acquire the form control that Angular created from the template by querying for it. When a field loses focus, the blur event is fired, and at that point, we can do custom validation or we could just evaluate the entire form and modify the user interface as needed. form.get('username').touched", "form.hasError('invalidusername', ['username']) && Angular does not offer this kind of validation: hence we will create the custom validator to validate the user's age. Copyright 2017 to present - tiepphan.com All rights reserved. Here is the rest of the directive to help you get an idea of how it all comes together: If you are familiar with Angular validations, you may have noticed the template grows more slowly and proportionally. These cookies will be stored in your browser only with your consent. * If no path is given, it checks for the error on the present control. The cookie is used to store the user consent for the cookies in the category "Other. This command will create the Angular project with the name angular-forms-validation. We looked at below. You have to write the control model and manage it. A validator can be plugged in directly into a reactive form simply by adding it to the validators list. When an element is invalid, the following things are true: If the user tries to send the data, the browser will block the form and display an error message. All rights reserved. A validation of the suggested method was carried out based on the results from a comprehensive experiment that included both satellite imaging of the Black Sea water area and sea truth . Then I can run the following code which only seems to reset the validity of controls rather than fiddling with the underlying data: Object.keys(this.myForm.controls).forEach((key) => { const control = this.myForm.controls[key]; control.markAsPristine(); control.markAsUntouched(); }); This runs through every control and resets the validity . There's a lot of JavaScript logic in the HTML. If a directive defines a custom validator and that directive is destroyed, the form control should no longer undergo the validation associated with the directive (but it does). const control = new FormControl('', Validators.required); Those validators are very helpful because they allow us to perform standard form validation. The