Normally when I have complex validation in reactive forms I would define a formGroup for those controls that depend on each other.
This is not possible in the above senario, where we have 3 steps = 3 groups
and the depending 3 fields firstUnique, secondUnique, thirdUnique
.
<form [formGroup]="myForm">
<mat-horizontal-stepper formArrayName="formArray" #stepper>
<mat-step formGroupName="0" [stepControl]="formArray?.get([0])" errorMessage="Name is required.">
<ng-template matStepLabel>Fill out your name</ng-template>
<mat-form-field>
<input matInput placeholder="Last name, First name" formControlName="firstCtrl" required>
</mat-form-field>
<mat-form-field>
<input matInput placeholder="UNIQUE1" formControlName="firstUnique" required>
</mat-form-field>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>
<mat-step formGroupName="1" [stepControl]="formArray?.get([1])" errorMessage="Address is required.">
<ng-template matStepLabel>Fill out your address</ng-template>
<mat-form-field>
<input matInput placeholder="Address" formControlName="secondCtrl" required>
</mat-form-field>
<mat-form-field>
<input matInput placeholder="UNIQUE2" formControlName="secondUnique" required>
</mat-form-field>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>
<mat-step formGroupName="2" [stepControl]="formArray?.get([2])" errorMessage="Error!">
<ng-template matStepLabel>Done</ng-template>
You are now done.
<div>
<mat-form-field>
<input matInput placeholder="UNIQUE3" formControlName="thirdUnique" required>
</mat-form-field>
<button mat-button matStepperPrevious>Back</button>
<button mat-button (click)="stepper.reset()">Reset</button>
</div>
</mat-step>
</mat-horizontal-stepper>
I use techniques described SO_answer and Material_docs
My Solution is working so far, but im not satisfied with it:
- On startup
Unique Validation
is run a thousand times (30-40 times) (hacky) - On EVERY change of ANY input in the whole stepper the
Unique Validation
is trigger. (this is because I had to add it to the whole formGroup). A simple task as those 3 input field need to be UNIQUE has become a boilerplatey and complex mess. (please observe the
function Unique(arr: string[])
)When a correct step gets invalid by the
UNIQUE Validator
or the STEP gets valid again the STEPPER-VALIDATION is not invoked. (example: firstUnique = "a", secondUnique "b", thirdUnique = "a" (again) )
MyForm
this.myForm = this._formBuilder.group({
formArray:
this._formBuilder.array([
this._formBuilder.group({
firstCtrl: [''],
firstUnique: [''],
}),
this._formBuilder.group({
secondCtrl: [''],
secondUnique: [''],
}),
this._formBuilder.group({
thirdUnique: [''],
})
])
}, {
validator: [Unique(['0;firstUnique', '1;secondUnique', '2;thirdUnique'])]
});
Unique Validator fun
function Unique(arr: string[]) {
const validKey = "uniqueValid";
return (formGroup: FormGroup) => {
const myValues =
arr.map(path => {
const s = path.split(';');
return (<FormArray>formGroup.get('formArray'))
.controls[parseInt(s[0])]
.controls[s[1]];
});
const myKeys = arr.map(path => path.split(';')[1] )
const obj = {};
myKeys.forEach(function (k, i) {
obj[k] = myValues[i];
})
myKeys.forEach((item, index) => {
debugger
console.log('unique validation function runs')
const control = obj[item];
const tmp = myKeys.slice();
tmp.splice(index,1);
const ans = tmp
.filter( el => obj[item].value === obj[el].value)
if ( ans.length && control.value ) {
const err = {}
err[validKey] = `identicial to: ${ans.join(', ')}`
control.setErrors(err);
} else if ( obj[item].errors && !obj[item].errors[validKey] ) {
return;
} else {
control.setErrors(null);
}
})
}