I have this function that check my form fields validity.
/**
* Return boolean indicating current slides validity
*/
public hIsCurrentSlideValid(): boolean {
/* Use the current active slide to determine if the datafield being displayed on that slide has error checks */
let lAnyErrorChecks = this.myForm.controls[this.hActiveSlideIndex].errors;
/* If there are error check, ensure that error check is the required field and is active else enable the button */
return lAnyErrorChecks ? (lAnyErrorChecks.required as boolean) : false;
}
Just to make things a little more complicated the active index check return a promise so that cant be used in html without triggering a endless loop of updates
So I had to add a function that stores the Active index off the slide when the slide is changed
/**
* Return values of current slide on change
*/
public hCheckSlidesOnChange() {
let lPromiseBeginSlide = this.slides.isBeginning();
let lPromiseEndSlide = this.slides.isEnd();
/* Get the current active slide */
let lPromiseActiveIndex = this.slides.getActiveIndex();
Promise.all([lPromiseBeginSlide, lPromiseEndSlide, lPromiseActiveIndex]).then((aPromiseReturn) => {
aPromiseReturn[0]
? (this.disablePreviousButton = true)
: (this.disablePreviousButton = false);
aPromiseReturn[1] ? (this.disableNextButton = true) : (this.hSlidesDisableNextButton = false);
/* Get the current active slide */
this.hActiveSlideIndex = aPromiseReturn[2];
});
}
For completeness here is my global variables.
@ViewChild("mySlides") slides;
public myForm: FormGroup = new FormGroup({});
public disableNextButton: boolean = false;
public disablePreviousButton: boolean = true;
public hActiveSlideIndex: number = 0;
Here is my HTML
<form id="data-collector" [formGroup]="myForm">
<ion-slides #mySlides (ionSlideWillChange)="hCheckSlidesOnChange()">
<ion-slide *ngFor="let myValue of myValues" class="form-row">
<myinputComponent></myinputComponent>
</ion-slide>
</ion-slides>
</form>
<ion-footer>
<ion-button slot="start"
[disabled]="mySlides ? disablePreviousButton : true"
(click)="mySlides.slidePrev()">Previous</ion-button>
<ion-button slot="end"
[hidden]="mySlides ? disableNextButton : true"
[disabled]="hIsCurrentSlideValid()" (click)="mySlides.slideNext()">Next</ion-button>
<ion-button form="data-collector" slot="end" type="submit"
[hidden]="mySlides ? !disableNextButton : true" [disabled]="isFormValid" (click)="Save()">
Save
</ion-button>
</ion-footer>
Let me know if there is anything that is unclear, but yea this is working for me