I have several form arrays and I need a validation so that a particular field in each form row has to be unique across all the form arrays. If any of the values appear more than once, both form fields have to be marked in red.
I managed to write a function so that if there are any changes to those fields, the function returns true/false. But I'm unsure how to use this for the actual validation process.
component.html:
<div formArrayName="temperatureFormArr">
<div class="row" *ngFor="let temperature of parentForm['controls'].detailForm['controls'].temperatureFormArr['controls']; let i=index"
[formGroupName]="i">
<div class="col-4">
<mat-form-field appearance="outline" class="fex-input">
<mat-label>Higher Level Function</mat-label>
<input autocomplete = "off" matInput placeholder="Higher Level Function" (input)="testFunction(i, 'temperatureFormArr')" formControlName="higherLevelFunction">
</mat-form-field>
</div>
<div class="col-6">
<mat-form-field appearance="outline" class="fex-input">
<mat-label>Description</mat-label>
<input autocomplete = "off" matInput placeholder="Description" formControlName="description">
</mat-form-field>
</div>
</div>
</div>
<div formArrayName="waterPressureFormArr">
<div class="row" *ngFor="let waterPressure of parentForm['controls'].detailForm['controls'].waterPressureFormArr['controls']; let i=index"
[formGroupName]="i">
<div class="col-4">
<mat-form-field appearance="outline" class="fex-input">
<mat-label>Higher Level Function</mat-label>
<input autocomplete = "off" matInput placeholder="Higher Level Function" (input)="testFunction(i, 'waterPressureFormArr')" formControlName="higherLevelFunction">
</mat-form-field>
</div>
<div class="col-6">
<mat-form-field appearance="outline" class="fex-input">
<mat-label>Description</mat-label>
<input autocomplete = "off" matInput placeholder="Description" formControlName="description">
</mat-form-field>
</div>
</div>
</div>
component.ts
testFunction(i: any, typeOfArray: string) {
var duplicateFlag = false;
var testValue = this.parentForm['controls'].detailForm['controls'][typeOfArray].controls[i].value;
for (let index = 0; index < this.parentForm['controls'].detailForm['controls'].temperatureFormArr.length; index++) {
if(this.parentForm['controls'].detailForm['controls'].temperatureFormArr['controls'][index]
.get('higherLevelFunction').value == testValue.higherLevelFunction && (index != i || typeOfArray != 'temperatureFormArr')) {
duplicateFlag = true;
}
}
for (let index = 0; index < this.parentForm['controls'].detailForm['controls'].waterPressureFormArr.length; index++) {
if(this.parentForm['controls'].detailForm['controls'].waterPressureFormArr['controls'][index]
.get('higherLevelFunction').value == testValue.higherLevelFunction && (index != i || typeOfArray != 'waterPressureFormArr')) {
duplicateFlag = true;
}
}
return duplicateFlag;
}
For every keystroke, this function is called and a check is done against all the values in all other form arrays to see if there are any duplicates. But how do I change it so that I can use this function as a validator and mark the duplicate input fields accordingly?