difference between patchValue and updateValueAndValidity() method?

2.3k views Asked by At

What's the difference between patchValue and updateValueAndValidity. Do we need to call updateValueAndvalidity whenever we use patchValue or setValue?

1

There are 1 answers

0
knowledgeseeker On

From the angular documentation // https://angular.io/guide/reactive-forms

Partial model updates When updating the value for a form group instance that contains multiple controls, you may only want to update parts of the model. This section covers how to update specific parts of a form control data model.

Patching the model value There are two ways to update the model value:

Use the setValue() method to set a new value for an individual control. The setValue() method strictly adheres to the structure of the form group and replaces the entire value for the control.

Use the patchValue() method to replace any properties defined in the object that have changed in the form model. +++++++++++++++++++++++++++++++++++++++ Regarding the updateValueAndValidity

https://angular.io/api/forms/AbstractControl#updateValueAndValidity

Recalculates the value and validation status of the control.

based on my experiance if you dynamically changes your form validators you could you updateValueAndValidity . but if you just changes your model object then use only patchvalue

i would say updateValueAndValidity incorporates modal changes + validator changes on the form controls.