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>