My Ionic 3 form consists of 3 checkboxes fields:
<form [formGroup]="bookingForm" (ngSubmit)="addBooking()">
<ion-card>
<ion-card-content>
<ion-item-group formGroupName="period">
<ion-list>
<ion-list-header>
Select the Period:
</ion-list-header>
<ion-item>
<ion-label>Morning</ion-label>
<ion-checkbox formControlName="morning"></ion-checkbox>
</ion-item>
<ion-item>
<ion-label>Afternoon</ion-label>
<ion-checkbox formControlName="afternoon"></ion-checkbox>
</ion-item>
<ion-item>
<ion-label>Night</ion-label>
<ion-checkbox formControlName="night"></ion-checkbox>
</ion-item>
</ion-list>
</ion-item-group>
</ion-card-content>
</ion-card>
<button ion-button color="primary" block type="submit" [disabled]="!bookingForm.valid">Add</button>
I aim to ensure that at least one checkbox is selected by the user.
This snippet from my TS file includes the form builder code:
constructor(
private formBuilder: FormBuilder
) {
this.bookingForm = this.formBuilder.group({
//... other fields in the form ...
period: this.formBuilder.group(
{
morning: false,
afternoon: false,
night: false
},
Validators.requiredTrue
)
});
}
The validator isn't functioning as intended. The Add button remains enabled by default, allowing users to submit the form without selecting any checkbox.