Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - FormControl setValue 'onlySelf' parameter

Trying to understand what the 'onlySelf' parameter does when passing to setValue.

this.form.get('name').setValue('', { onlySelf: true }) 

The documentation says: "If onlySelf is true, this change will only affect the validation of this FormControl and not its parent component. This defaults to false."

However I'm struggling to understand this. Still fairly new to the using Angulars' model driven forms.

like image 313
leepowell Avatar asked Sep 20 '16 20:09

leepowell


People also ask

What is onlySelf Angular?

onlySelf : When true, each change only affects this control, and not its parent. Default is false. emitEvent : When true or not supplied (the default), both the statusChanges and valueChanges observables emit events with the latest status and value when the control is reset. When false, no events are emitted.

Does patchValue set dirty?

Bookmark this question.

What is the difference between setValue and patchValue?

SetValue Vs PatchValue The difference is that with setValue we must include all the controls, while with the patchValue you can exclude some controls.


2 Answers

Angular2 by default will check for the form control/form group validity cascadingly up to the top level whenever there's an update to any form element value, unless you say no. onlySelf is the tool to help you do that.

Say you have a loginForm that has a username field and a password field, both of them are required, like this:

this.userNameControl = this.formBuilder.control('Harry', Validators.required); this.passwordControl = this.formBuilder.control('S3cReT', Validators.required); this.loginForm = this.formBuilder.group({   userName: this.userNameControl,   password: this.passwordControl }); 

After this code, this.loginForm.valid is true.

If you set the value of a control using the default setting (onlySelf = false), Angular2 will update the control's validity as well as form group's validity. For example, this:

this.passwordControl.setValue(''); 

will result in

this.passwordControl.valid === false this.loginForm.valid === false 

However, this:

this.passwordControl.setValue('', { onlySelf: true }); 

will only change passwordControl's validity only:

this.passwordControl.valid === false this.loginForm.valid === true 
like image 146
Harry Ninh Avatar answered Oct 02 '22 15:10

Harry Ninh


Put it this way, let's say that you have a form, called mainForm which is valid. It has four controls on it and all four have a value. Now, you decide to update the value of one of your controls, let's say you update it to some incorrect value and you specify onlySelf: true. If you try to call this.mainForm.valid, you will get the result that your form is valid even though your control is not valid, and it's invalid state should not allow the form to be submitted. But because the forms valid property is reporting true, you will be submitting inconsistent values to the backend.

It might be confusing why you would have this property, but there might be occasions when you don't want to invalidate the form because of one value or control. Probably you have some advanced checks on the server and you want to correct the value on the server or you might depend on a value from some external web service that might not be available at the time. I'm sure there are number of scenarios but this is something from top of my head.

like image 39
Husein Roncevic Avatar answered Oct 02 '22 14:10

Husein Roncevic