I have a formarray consisting of a formgroup. How can I check that the fields inside the created formgroups are unique.
I have this form:
form: FormGroup = this.formBuilder.group({
fields: this.formBuilder.array([]),
});
private createField() {
return this.formBuilder.group({
label: ['', [Validators.required, uniqueLabelsValidator()]],
});
}
public addField() {
(this.form.get('fields') as FormArray).push(this.createField());
}
How can I check that all "label" inside "fields" are unique and if this is not the case, then assign a validation error to a field with a duplicate value
You may consider using the @rxweb/reactive-form-validators library.
Reference: Unique Validation (Note: The link also provides the code implementation for unique values without RxWeb validators)
RxReactiveFormsModuleinto the app module or standalone component.RxwebValidators.unique()validation withmessage(optional) into the form control.Demo @ StackBlitz