In Aangular-12, I am implementing dynamic input fields FormArray in Reactive Form. I have this code:
isLoading = false;
isSubmitted = false;
contactInfoForm!: FormGroup;
updateContact() {
this.contactInfoForm = this.fb.group({
id: [''],
current_residential_address: ['', [Validators.required]],
contacts: this.fb.array([
this.addContactFormGroup()
])
});
}
addContactFormGroup(): FormGroup {
return this.fb.group({
phone_type_id: ['', Validators.required],
phone_number: ['', [Validators.required, Validators.maxLength(15)]]
});
}
get fc() {
return this.contactInfoForm.controls;
};
public addContactButtonClick() {
const contacts = this.contactInfoForm.get('contacts') as FormArray
contacts.push(this.addContactFormGroup())
}
get contacts() {
return this.contactInfoForm.controls['contacts'] as FormArray;
}
getContactsFormArray(): FormArray {
return this.contactInfoForm.get('contacts') as FormArray;
}
get contactArray(): FormArray {
return <FormArray > this.contactInfoForm.get('contacts');
}
public removeOrClearContact(i: number) {
const contacts = this.contactInfoForm.get('contacts') as FormArray
if (contacts.length > 1) {
contacts.removeAt(i)
} else {
Swal.fire({
position: 'center',
icon: 'error',
title: 'Employee must have at least one Contact Phone No.',
showConfirmButton: true,
timer: 10000
});
contacts.reset()
}
}
onSubmitContact() {
this.isSubmitted = true;
if (this.contactInfoForm.invalid) {
return;
}
this.isLoading = true;
this.employeeService.updateContact(this._id, this.contactdata).subscribe(res => {
this.data = res;
});
}
<form [formGroup]="contactInfoForm" (ngSubmit)="onSubmitContact()">
<div class="row">
<div class="col-12 col-md-12">
<div class="form-group">
<label for="current_residential_address">Current Residential Address:<span style="color:red;">*</span></label>
<textarea rows="2" formControlName="current_residential_address" name="description" type="text" placeholder="22, Alexander Close ..." class="form-control mb-3" required>
</textarea>
</div>
<div *ngIf="fc.current_residential_address.touched && fc.current_residential_address.invalid">
<div *ngIf="fc.current_residential_address.hasError('required')">
<div class="text-danger">
Current Residential Address is required!
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div formArrayName="contacts" class="col-md-12">
<div *ngFor="let contact of getContactsFormArray().controls; let i = index" [formGroupName]="i">
<p>
<b>Contact Phone : {{i + 1}}</b>
</p>
<hr>
<div class="row">
<div class="col-12 col-md-4">
<div class="form-group">
<label for="phone_number">Phone Number:<span style="color:red;">*</span></label>
<div class="input-group mb-4">
<ngx-intl-tel-input [cssClass]="'form-control mb-4'" [preferredCountries]="preferredCountries" [enableAutoCountrySelect]="false" [enablePlaceholder]="true" [searchCountryFlag]="true" [searchCountryField]="[SearchCountryField.Iso2, SearchCountryField.Name]"
[selectFirstCountry]="false" [selectedCountryISO]="CountryISO.Scotland" [phoneValidation]="true" [separateDialCode]="true" name="phone_number" formControlName="phone_number">
</ngx-intl-tel-input>
</div>
</div>
<div *ngIf="getContactFormGroup(i).get('phone_number')!.touched && getContactFormGroup(i).get('phone_number')!.invalid">
<div *ngIf="getContactFormGroup(i).get('phone_number')!.hasError('required')">
<div class="text-danger">
Phone Number is required!
</div>
</div>
<div *ngIf="getContactFormGroup(i).get('phone_number')!.hasError('validatePhoneNumber')">
<div class="text-danger">
Invalid Phone Number!
</div>
</div>
</div>
</div>
<div class="col-12 col-md-4">
<div class="form-group">
<label for="phone_type_id">Phone Type:<span style="color:red;">*</span></label>
<ng-select [items]="phonetypes" [selectOnTab]="true" [searchable]="true" bindValue="id" bindLabel="type_name" placeholder="Select Phone Type" [multiple]="false" [clearable]="true" required formControlName="phone_type_id">
</ng-select>
</div>
</div>
<div class="col-12 col-md-2">
<div class="form-group">
<br><button type="button" class="btn btn-danger float-right" (click)="removeOrClearContact(i)"><i class="fas fa-times-circle"></i> Remove</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="card-footer">
<button type="button" class="btn btn-primary float-right" (click)="addContactButtonClick()"><i class="fas fa-plus-circle"></i> Add</button>
<button type="submit" class="btn btn-success" [disabled]="isLoading" class="btn btn-success" (click)="contactValidate()">
<span *ngIf="isLoading" class="spinner-border spinner-border-sm mr-1"></span>
<i class="fa fa-save" aria-hidden="true"></i> Save</button>
</div>
</form>
I discovered that the submit button is enabled when there is no dynamic input field FormArray.
How do I disable or hide submit button if there is no form array?
Thanks