Angular 7 setting values of reactive form on component load enable save button even if the form isn't...











up vote
1
down vote

favorite












I have the following issue. Click to check the stack blitz.



I am setting a reactive form in the constructor:



this.formGroup = this.fb.group({
'family_name_en': new FormControl('', Validators.required),
'family_name_ar': new FormControl('', [Validators.required, Validators.pattern('[u0600-u06FF ]*')]),
'unit_id': new FormControl({ value: '', disabled: true }, [Validators.required])
});


And in the ngOnInti() hook, I am getting the data related to the unit_id.



As I am migrating data from platform to another, the family name in arabic was not recommended, but it is now. So most of the data will load without the arabic name.



The issue is the button is always enabled, and what I need to do is if a user wants to update this unit_id, he should add the arabic name so the button is enabled then.



If the form is valid, no harm enabling the button on load of the component.



Here is the getData() method that get the data and set the form group controls values:



ngOnInit() {
this.getDataById();
}


getDataById():



getDataById() {
this.showSpinner = true;
this.auth.getDataById(this.unit_id).subscribe(
(data) => {
if (data['info'][0]['total'] == 1) {
this.noDataBool = false;
this.showSpinner = false;
this.family_name_en = data['info'][0]['hh_last_name_en'];
this.formGroup.controls['family_name_en'].setValue(this.family_name_en);
this.family_name_ar = data['info'][0]['hh_last_name_ar'];
this.formGroup.controls['family_name_ar'].setValue(this.family_name_ar);
this.unit_id = data['info'][0]['unit_id'];
this.formGroup.controls['unit_id '].setValue(this.unit_id);
}
}


The button:



<button mat-raised-button color="warn" (click)="updateData()" [disabled]="!formGroup.valid">
<mat-icon>update</mat-icon>Update
</button>









share|improve this question
























  • Would it be possible for you to create a minimal sample stackblitz replicating this issue?
    – SiddAjmera
    Nov 20 at 7:11










  • How to simulate the getDataById() ?
    – alim1990
    Nov 20 at 7:11










  • You can simply create a method on your component and return some static data that you are expecting from this method for that.
    – SiddAjmera
    Nov 20 at 7:12










  • @SiddAjmera Okay I will work on it now.
    – alim1990
    Nov 20 at 7:13










  • here is a stack blitz @SiddAjmera stackblitz.com/edit/…
    – alim1990
    Nov 20 at 7:28















up vote
1
down vote

favorite












I have the following issue. Click to check the stack blitz.



I am setting a reactive form in the constructor:



this.formGroup = this.fb.group({
'family_name_en': new FormControl('', Validators.required),
'family_name_ar': new FormControl('', [Validators.required, Validators.pattern('[u0600-u06FF ]*')]),
'unit_id': new FormControl({ value: '', disabled: true }, [Validators.required])
});


And in the ngOnInti() hook, I am getting the data related to the unit_id.



As I am migrating data from platform to another, the family name in arabic was not recommended, but it is now. So most of the data will load without the arabic name.



The issue is the button is always enabled, and what I need to do is if a user wants to update this unit_id, he should add the arabic name so the button is enabled then.



If the form is valid, no harm enabling the button on load of the component.



Here is the getData() method that get the data and set the form group controls values:



ngOnInit() {
this.getDataById();
}


getDataById():



getDataById() {
this.showSpinner = true;
this.auth.getDataById(this.unit_id).subscribe(
(data) => {
if (data['info'][0]['total'] == 1) {
this.noDataBool = false;
this.showSpinner = false;
this.family_name_en = data['info'][0]['hh_last_name_en'];
this.formGroup.controls['family_name_en'].setValue(this.family_name_en);
this.family_name_ar = data['info'][0]['hh_last_name_ar'];
this.formGroup.controls['family_name_ar'].setValue(this.family_name_ar);
this.unit_id = data['info'][0]['unit_id'];
this.formGroup.controls['unit_id '].setValue(this.unit_id);
}
}


The button:



<button mat-raised-button color="warn" (click)="updateData()" [disabled]="!formGroup.valid">
<mat-icon>update</mat-icon>Update
</button>









share|improve this question
























  • Would it be possible for you to create a minimal sample stackblitz replicating this issue?
    – SiddAjmera
    Nov 20 at 7:11










  • How to simulate the getDataById() ?
    – alim1990
    Nov 20 at 7:11










  • You can simply create a method on your component and return some static data that you are expecting from this method for that.
    – SiddAjmera
    Nov 20 at 7:12










  • @SiddAjmera Okay I will work on it now.
    – alim1990
    Nov 20 at 7:13










  • here is a stack blitz @SiddAjmera stackblitz.com/edit/…
    – alim1990
    Nov 20 at 7:28













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have the following issue. Click to check the stack blitz.



I am setting a reactive form in the constructor:



this.formGroup = this.fb.group({
'family_name_en': new FormControl('', Validators.required),
'family_name_ar': new FormControl('', [Validators.required, Validators.pattern('[u0600-u06FF ]*')]),
'unit_id': new FormControl({ value: '', disabled: true }, [Validators.required])
});


And in the ngOnInti() hook, I am getting the data related to the unit_id.



As I am migrating data from platform to another, the family name in arabic was not recommended, but it is now. So most of the data will load without the arabic name.



The issue is the button is always enabled, and what I need to do is if a user wants to update this unit_id, he should add the arabic name so the button is enabled then.



If the form is valid, no harm enabling the button on load of the component.



Here is the getData() method that get the data and set the form group controls values:



ngOnInit() {
this.getDataById();
}


getDataById():



getDataById() {
this.showSpinner = true;
this.auth.getDataById(this.unit_id).subscribe(
(data) => {
if (data['info'][0]['total'] == 1) {
this.noDataBool = false;
this.showSpinner = false;
this.family_name_en = data['info'][0]['hh_last_name_en'];
this.formGroup.controls['family_name_en'].setValue(this.family_name_en);
this.family_name_ar = data['info'][0]['hh_last_name_ar'];
this.formGroup.controls['family_name_ar'].setValue(this.family_name_ar);
this.unit_id = data['info'][0]['unit_id'];
this.formGroup.controls['unit_id '].setValue(this.unit_id);
}
}


The button:



<button mat-raised-button color="warn" (click)="updateData()" [disabled]="!formGroup.valid">
<mat-icon>update</mat-icon>Update
</button>









share|improve this question















I have the following issue. Click to check the stack blitz.



I am setting a reactive form in the constructor:



this.formGroup = this.fb.group({
'family_name_en': new FormControl('', Validators.required),
'family_name_ar': new FormControl('', [Validators.required, Validators.pattern('[u0600-u06FF ]*')]),
'unit_id': new FormControl({ value: '', disabled: true }, [Validators.required])
});


And in the ngOnInti() hook, I am getting the data related to the unit_id.



As I am migrating data from platform to another, the family name in arabic was not recommended, but it is now. So most of the data will load without the arabic name.



The issue is the button is always enabled, and what I need to do is if a user wants to update this unit_id, he should add the arabic name so the button is enabled then.



If the form is valid, no harm enabling the button on load of the component.



Here is the getData() method that get the data and set the form group controls values:



ngOnInit() {
this.getDataById();
}


getDataById():



getDataById() {
this.showSpinner = true;
this.auth.getDataById(this.unit_id).subscribe(
(data) => {
if (data['info'][0]['total'] == 1) {
this.noDataBool = false;
this.showSpinner = false;
this.family_name_en = data['info'][0]['hh_last_name_en'];
this.formGroup.controls['family_name_en'].setValue(this.family_name_en);
this.family_name_ar = data['info'][0]['hh_last_name_ar'];
this.formGroup.controls['family_name_ar'].setValue(this.family_name_ar);
this.unit_id = data['info'][0]['unit_id'];
this.formGroup.controls['unit_id '].setValue(this.unit_id);
}
}


The button:



<button mat-raised-button color="warn" (click)="updateData()" [disabled]="!formGroup.valid">
<mat-icon>update</mat-icon>Update
</button>






angular angular6 angular-reactive-forms angular7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 7:28

























asked Nov 20 at 7:01









alim1990

93711232




93711232












  • Would it be possible for you to create a minimal sample stackblitz replicating this issue?
    – SiddAjmera
    Nov 20 at 7:11










  • How to simulate the getDataById() ?
    – alim1990
    Nov 20 at 7:11










  • You can simply create a method on your component and return some static data that you are expecting from this method for that.
    – SiddAjmera
    Nov 20 at 7:12










  • @SiddAjmera Okay I will work on it now.
    – alim1990
    Nov 20 at 7:13










  • here is a stack blitz @SiddAjmera stackblitz.com/edit/…
    – alim1990
    Nov 20 at 7:28


















  • Would it be possible for you to create a minimal sample stackblitz replicating this issue?
    – SiddAjmera
    Nov 20 at 7:11










  • How to simulate the getDataById() ?
    – alim1990
    Nov 20 at 7:11










  • You can simply create a method on your component and return some static data that you are expecting from this method for that.
    – SiddAjmera
    Nov 20 at 7:12










  • @SiddAjmera Okay I will work on it now.
    – alim1990
    Nov 20 at 7:13










  • here is a stack blitz @SiddAjmera stackblitz.com/edit/…
    – alim1990
    Nov 20 at 7:28
















Would it be possible for you to create a minimal sample stackblitz replicating this issue?
– SiddAjmera
Nov 20 at 7:11




Would it be possible for you to create a minimal sample stackblitz replicating this issue?
– SiddAjmera
Nov 20 at 7:11












How to simulate the getDataById() ?
– alim1990
Nov 20 at 7:11




How to simulate the getDataById() ?
– alim1990
Nov 20 at 7:11












You can simply create a method on your component and return some static data that you are expecting from this method for that.
– SiddAjmera
Nov 20 at 7:12




You can simply create a method on your component and return some static data that you are expecting from this method for that.
– SiddAjmera
Nov 20 at 7:12












@SiddAjmera Okay I will work on it now.
– alim1990
Nov 20 at 7:13




@SiddAjmera Okay I will work on it now.
– alim1990
Nov 20 at 7:13












here is a stack blitz @SiddAjmera stackblitz.com/edit/…
– alim1990
Nov 20 at 7:28




here is a stack blitz @SiddAjmera stackblitz.com/edit/…
– alim1990
Nov 20 at 7:28












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Instead of directly accessing the FormControls by using controls on the formGroup, you should use the get API on them. This would give you more control and significantly clean up your implementation(especially in case of getting deeply nested elements). You can change the Component Implementation like this:



import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { Observable } from 'rxjs/Observable';

@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {

formGroup: FormGroup;
titleAlert: string = 'This field is required';
post: any = '';
family_name_en;
family_name_ar;
unit_id;
constructor(private fb: FormBuilder) { }

ngOnInit() {
this.formGroup = this.fb.group({
'family_name_en': new FormControl('', Validators.required),
'family_name_ar': new FormControl('', [Validators.required, Validators.pattern('[u0600-u06FF ]*')]),
'unit_id': new FormControl({ value: '', disabled: true }, [Validators.required])
});
this.getDataById();
}

getDataById() {
let data: Array<any> = [
'Ali', '', '123'
]
this.family_name_en = data[0];
this.formGroup.get('family_name_en').setValue(this.family_name_en);
this.family_name_ar = data[1];
this.formGroup.get('family_name_ar').setValue(this.family_name_ar);
this.unit_id = data[2];
this.formGroup.get('unit_id').setValue(this.unit_id);
}

}


Here's an Updated StackBlitz for your ref. You can test it by typing in علي in there which would enable the Update button. Typing Ali will let it stay disabled.






share|improve this answer























  • What is the difference between get() and .constrols.setValue() ?
    – alim1990
    Nov 20 at 8:08










  • This should give you a pretty good idea about it.
    – SiddAjmera
    Nov 20 at 8:10


















up vote
1
down vote













I always use the following code in situations like this:



this.family_name_ar.setValue(data['info'][0]['hh_last_name_ar']);
this.family_name_ar.updateValueAndValidity();


By calling the second method, updateValueAndValidity() we tell Angular to




Recalculate the value and validation status of the control.




More info on the Angular Docs.



Now, the button should be disabled and the form invalid if the data['info'][0]['hh_last_name_ar'] is empty.



You can use the methods above for every field you need.






share|improve this answer























  • What is the code of updateValueAndValidity() ?
    – alim1990
    Nov 20 at 7:12










  • It's a method on type AbstractControl.
    – SiddAjmera
    Nov 20 at 7:13










  • Updated the answer with the link for the docs.
    – Mihail Stancescu
    Nov 20 at 7:14










  • @MihailStancescu I tried it and the button still disabled as `[disabled]="!formGroup.valid" is always false
    – alim1990
    Nov 20 at 7:39










  • You have to do what I said for all the controls.
    – Mihail Stancescu
    Nov 20 at 7:51











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387798%2fangular-7-setting-values-of-reactive-form-on-component-load-enable-save-button-e%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Instead of directly accessing the FormControls by using controls on the formGroup, you should use the get API on them. This would give you more control and significantly clean up your implementation(especially in case of getting deeply nested elements). You can change the Component Implementation like this:



import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { Observable } from 'rxjs/Observable';

@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {

formGroup: FormGroup;
titleAlert: string = 'This field is required';
post: any = '';
family_name_en;
family_name_ar;
unit_id;
constructor(private fb: FormBuilder) { }

ngOnInit() {
this.formGroup = this.fb.group({
'family_name_en': new FormControl('', Validators.required),
'family_name_ar': new FormControl('', [Validators.required, Validators.pattern('[u0600-u06FF ]*')]),
'unit_id': new FormControl({ value: '', disabled: true }, [Validators.required])
});
this.getDataById();
}

getDataById() {
let data: Array<any> = [
'Ali', '', '123'
]
this.family_name_en = data[0];
this.formGroup.get('family_name_en').setValue(this.family_name_en);
this.family_name_ar = data[1];
this.formGroup.get('family_name_ar').setValue(this.family_name_ar);
this.unit_id = data[2];
this.formGroup.get('unit_id').setValue(this.unit_id);
}

}


Here's an Updated StackBlitz for your ref. You can test it by typing in علي in there which would enable the Update button. Typing Ali will let it stay disabled.






share|improve this answer























  • What is the difference between get() and .constrols.setValue() ?
    – alim1990
    Nov 20 at 8:08










  • This should give you a pretty good idea about it.
    – SiddAjmera
    Nov 20 at 8:10















up vote
2
down vote



accepted










Instead of directly accessing the FormControls by using controls on the formGroup, you should use the get API on them. This would give you more control and significantly clean up your implementation(especially in case of getting deeply nested elements). You can change the Component Implementation like this:



import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { Observable } from 'rxjs/Observable';

@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {

formGroup: FormGroup;
titleAlert: string = 'This field is required';
post: any = '';
family_name_en;
family_name_ar;
unit_id;
constructor(private fb: FormBuilder) { }

ngOnInit() {
this.formGroup = this.fb.group({
'family_name_en': new FormControl('', Validators.required),
'family_name_ar': new FormControl('', [Validators.required, Validators.pattern('[u0600-u06FF ]*')]),
'unit_id': new FormControl({ value: '', disabled: true }, [Validators.required])
});
this.getDataById();
}

getDataById() {
let data: Array<any> = [
'Ali', '', '123'
]
this.family_name_en = data[0];
this.formGroup.get('family_name_en').setValue(this.family_name_en);
this.family_name_ar = data[1];
this.formGroup.get('family_name_ar').setValue(this.family_name_ar);
this.unit_id = data[2];
this.formGroup.get('unit_id').setValue(this.unit_id);
}

}


Here's an Updated StackBlitz for your ref. You can test it by typing in علي in there which would enable the Update button. Typing Ali will let it stay disabled.






share|improve this answer























  • What is the difference between get() and .constrols.setValue() ?
    – alim1990
    Nov 20 at 8:08










  • This should give you a pretty good idea about it.
    – SiddAjmera
    Nov 20 at 8:10













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Instead of directly accessing the FormControls by using controls on the formGroup, you should use the get API on them. This would give you more control and significantly clean up your implementation(especially in case of getting deeply nested elements). You can change the Component Implementation like this:



import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { Observable } from 'rxjs/Observable';

@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {

formGroup: FormGroup;
titleAlert: string = 'This field is required';
post: any = '';
family_name_en;
family_name_ar;
unit_id;
constructor(private fb: FormBuilder) { }

ngOnInit() {
this.formGroup = this.fb.group({
'family_name_en': new FormControl('', Validators.required),
'family_name_ar': new FormControl('', [Validators.required, Validators.pattern('[u0600-u06FF ]*')]),
'unit_id': new FormControl({ value: '', disabled: true }, [Validators.required])
});
this.getDataById();
}

getDataById() {
let data: Array<any> = [
'Ali', '', '123'
]
this.family_name_en = data[0];
this.formGroup.get('family_name_en').setValue(this.family_name_en);
this.family_name_ar = data[1];
this.formGroup.get('family_name_ar').setValue(this.family_name_ar);
this.unit_id = data[2];
this.formGroup.get('unit_id').setValue(this.unit_id);
}

}


Here's an Updated StackBlitz for your ref. You can test it by typing in علي in there which would enable the Update button. Typing Ali will let it stay disabled.






share|improve this answer














Instead of directly accessing the FormControls by using controls on the formGroup, you should use the get API on them. This would give you more control and significantly clean up your implementation(especially in case of getting deeply nested elements). You can change the Component Implementation like this:



import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { Observable } from 'rxjs/Observable';

@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {

formGroup: FormGroup;
titleAlert: string = 'This field is required';
post: any = '';
family_name_en;
family_name_ar;
unit_id;
constructor(private fb: FormBuilder) { }

ngOnInit() {
this.formGroup = this.fb.group({
'family_name_en': new FormControl('', Validators.required),
'family_name_ar': new FormControl('', [Validators.required, Validators.pattern('[u0600-u06FF ]*')]),
'unit_id': new FormControl({ value: '', disabled: true }, [Validators.required])
});
this.getDataById();
}

getDataById() {
let data: Array<any> = [
'Ali', '', '123'
]
this.family_name_en = data[0];
this.formGroup.get('family_name_en').setValue(this.family_name_en);
this.family_name_ar = data[1];
this.formGroup.get('family_name_ar').setValue(this.family_name_ar);
this.unit_id = data[2];
this.formGroup.get('unit_id').setValue(this.unit_id);
}

}


Here's an Updated StackBlitz for your ref. You can test it by typing in علي in there which would enable the Update button. Typing Ali will let it stay disabled.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 8:12

























answered Nov 20 at 7:58









SiddAjmera

11.9k21137




11.9k21137












  • What is the difference between get() and .constrols.setValue() ?
    – alim1990
    Nov 20 at 8:08










  • This should give you a pretty good idea about it.
    – SiddAjmera
    Nov 20 at 8:10


















  • What is the difference between get() and .constrols.setValue() ?
    – alim1990
    Nov 20 at 8:08










  • This should give you a pretty good idea about it.
    – SiddAjmera
    Nov 20 at 8:10
















What is the difference between get() and .constrols.setValue() ?
– alim1990
Nov 20 at 8:08




What is the difference between get() and .constrols.setValue() ?
– alim1990
Nov 20 at 8:08












This should give you a pretty good idea about it.
– SiddAjmera
Nov 20 at 8:10




This should give you a pretty good idea about it.
– SiddAjmera
Nov 20 at 8:10












up vote
1
down vote













I always use the following code in situations like this:



this.family_name_ar.setValue(data['info'][0]['hh_last_name_ar']);
this.family_name_ar.updateValueAndValidity();


By calling the second method, updateValueAndValidity() we tell Angular to




Recalculate the value and validation status of the control.




More info on the Angular Docs.



Now, the button should be disabled and the form invalid if the data['info'][0]['hh_last_name_ar'] is empty.



You can use the methods above for every field you need.






share|improve this answer























  • What is the code of updateValueAndValidity() ?
    – alim1990
    Nov 20 at 7:12










  • It's a method on type AbstractControl.
    – SiddAjmera
    Nov 20 at 7:13










  • Updated the answer with the link for the docs.
    – Mihail Stancescu
    Nov 20 at 7:14










  • @MihailStancescu I tried it and the button still disabled as `[disabled]="!formGroup.valid" is always false
    – alim1990
    Nov 20 at 7:39










  • You have to do what I said for all the controls.
    – Mihail Stancescu
    Nov 20 at 7:51















up vote
1
down vote













I always use the following code in situations like this:



this.family_name_ar.setValue(data['info'][0]['hh_last_name_ar']);
this.family_name_ar.updateValueAndValidity();


By calling the second method, updateValueAndValidity() we tell Angular to




Recalculate the value and validation status of the control.




More info on the Angular Docs.



Now, the button should be disabled and the form invalid if the data['info'][0]['hh_last_name_ar'] is empty.



You can use the methods above for every field you need.






share|improve this answer























  • What is the code of updateValueAndValidity() ?
    – alim1990
    Nov 20 at 7:12










  • It's a method on type AbstractControl.
    – SiddAjmera
    Nov 20 at 7:13










  • Updated the answer with the link for the docs.
    – Mihail Stancescu
    Nov 20 at 7:14










  • @MihailStancescu I tried it and the button still disabled as `[disabled]="!formGroup.valid" is always false
    – alim1990
    Nov 20 at 7:39










  • You have to do what I said for all the controls.
    – Mihail Stancescu
    Nov 20 at 7:51













up vote
1
down vote










up vote
1
down vote









I always use the following code in situations like this:



this.family_name_ar.setValue(data['info'][0]['hh_last_name_ar']);
this.family_name_ar.updateValueAndValidity();


By calling the second method, updateValueAndValidity() we tell Angular to




Recalculate the value and validation status of the control.




More info on the Angular Docs.



Now, the button should be disabled and the form invalid if the data['info'][0]['hh_last_name_ar'] is empty.



You can use the methods above for every field you need.






share|improve this answer














I always use the following code in situations like this:



this.family_name_ar.setValue(data['info'][0]['hh_last_name_ar']);
this.family_name_ar.updateValueAndValidity();


By calling the second method, updateValueAndValidity() we tell Angular to




Recalculate the value and validation status of the control.




More info on the Angular Docs.



Now, the button should be disabled and the form invalid if the data['info'][0]['hh_last_name_ar'] is empty.



You can use the methods above for every field you need.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 7:14

























answered Nov 20 at 7:11









Mihail Stancescu

3,39911018




3,39911018












  • What is the code of updateValueAndValidity() ?
    – alim1990
    Nov 20 at 7:12










  • It's a method on type AbstractControl.
    – SiddAjmera
    Nov 20 at 7:13










  • Updated the answer with the link for the docs.
    – Mihail Stancescu
    Nov 20 at 7:14










  • @MihailStancescu I tried it and the button still disabled as `[disabled]="!formGroup.valid" is always false
    – alim1990
    Nov 20 at 7:39










  • You have to do what I said for all the controls.
    – Mihail Stancescu
    Nov 20 at 7:51


















  • What is the code of updateValueAndValidity() ?
    – alim1990
    Nov 20 at 7:12










  • It's a method on type AbstractControl.
    – SiddAjmera
    Nov 20 at 7:13










  • Updated the answer with the link for the docs.
    – Mihail Stancescu
    Nov 20 at 7:14










  • @MihailStancescu I tried it and the button still disabled as `[disabled]="!formGroup.valid" is always false
    – alim1990
    Nov 20 at 7:39










  • You have to do what I said for all the controls.
    – Mihail Stancescu
    Nov 20 at 7:51
















What is the code of updateValueAndValidity() ?
– alim1990
Nov 20 at 7:12




What is the code of updateValueAndValidity() ?
– alim1990
Nov 20 at 7:12












It's a method on type AbstractControl.
– SiddAjmera
Nov 20 at 7:13




It's a method on type AbstractControl.
– SiddAjmera
Nov 20 at 7:13












Updated the answer with the link for the docs.
– Mihail Stancescu
Nov 20 at 7:14




Updated the answer with the link for the docs.
– Mihail Stancescu
Nov 20 at 7:14












@MihailStancescu I tried it and the button still disabled as `[disabled]="!formGroup.valid" is always false
– alim1990
Nov 20 at 7:39




@MihailStancescu I tried it and the button still disabled as `[disabled]="!formGroup.valid" is always false
– alim1990
Nov 20 at 7:39












You have to do what I said for all the controls.
– Mihail Stancescu
Nov 20 at 7:51




You have to do what I said for all the controls.
– Mihail Stancescu
Nov 20 at 7:51


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387798%2fangular-7-setting-values-of-reactive-form-on-component-load-enable-save-button-e%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen