Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between patchValue and updateValueAndValidity() method?

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

like image 792
Rahul Rai Avatar asked Sep 15 '26 19:09

Rahul Rai


1 Answers

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.

like image 70
knowledgeseeker Avatar answered Sep 17 '26 09:09

knowledgeseeker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!