patchvalue or setvalue of formbuilder does not mark field as dirty or touched

Hacker picture Hacker · Dec 19, 2017 · Viewed 8.5k times · Source

I have a multi step form where user traverses back and forth to the form. I save the form data in service and when he comes back i use patchvalue to patch all the data to form. I tried setvalue also, but the form fields are not marked as either dirty or touched. How to i mark fields updated as dirty and touched?

this.formBuilder.pathValue(formData);

Answer

Pankaj Parkar picture Pankaj Parkar · Dec 19, 2017

You could explicitly mark the form using markAsDirty() & markAsTouched() method over your form object. See API Here

this.formName.markAsDirty()
this.formName.markAsTouched()

Update

Angular 8 onwards you can use markAllAsTouched to mark all form field as touched

this.formName.markAllAsTouched()