angularangular9angular-template-form

How to manually set an Angular template driven form as invalid in angular9


I want to manually set the EmployeeForm as Invalid from the .ts file. Here EmployeeForm is a Template driven form.

I have tried to do the following but it didn't work.

this.EmployeeForm.setErrors({ 'invalid': true }); 

error message says:

Property 'setErrors' does not exist on type 'NgForm'. Did you mean 'getError'?

The same code works when I use it with other forms which are Reactive forms


Solution

  • Finally this worked

    this.EmployeeForm.form.setErrors({ 'invalid': true });