intersectionValidator

The intersectionValidator is a custom Angular Forms validator designed to ensure that two form controls do not have overlapping values. For example, you might have two lists of items and want to ensure they don't contain any duplicates. When there's any overlap between the arrays, both fields and the form will have the {intersection: true} error.

Examples

Example 1: Validate Non-Overlapping Arrays

Ensure that included namespaces and excluded namespaces do not overlap. Both fields must return arrays.

<form clrForm [formGroup]="form">
  <clr-select-container>
    <label>Included Namespaces</label>
    <select clrSelect multiple name="options" [formControl]="form.controls.includedNamespaces" required>
      <option value="default">default</option>
      <option value="kube-system">kube-system</option>
      <option value="kube-public">kube-public</option>
    </select>
    <clr-control-error *clrIfError="'required'">Required</clr-control-error>
    <clr-control-error *clrIfError="'intersection'">
      Included namespaces and excluded namespaces cannot overlap
    </clr-control-error>
  </clr-select-container>

  <clr-select-container>
    <label>Excluded Namespaces</label>
    <select clrSelect multiple name="options" [formControl]="form.controls.excludedNamespaces" required>
      <option value="default">default</option>
      <option value="kube-system">kube-system</option>
      <option value="kube-public">kube-public</option>
    </select>
    <clr-control-error *clrIfError="'required'">Required</clr-control-error>
    <clr-control-error *clrIfError="'intersection'">
      Included namespaces and excluded namespaces cannot overlap
    </clr-control-error>
  </clr-select-container>

  @if (form.errors?.['intersection']) {
    <cll-alert class="my-4" [content]="'Included namespaces and excluded namespaces have overlaps'" />
  }

  <button type="submit" class="btn-primary btn" [disabled]="form.invalid">Submit</button>
</form>
import {intersectionValidator} from 'ngx-lift';
import {Component} from '@angular/core';
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';

@Component({
  imports: [ReactiveFormsModule],
})
export class IntersectionValidatorComponent {
  form = new FormGroup(
    {
      includedNamespaces: new FormControl<string[]>([], Validators.required),
      excludedNamespaces: new FormControl<string[]>([], Validators.required),
    },
    {
      validators: [intersectionValidator('includedNamespaces', 'excludedNamespaces')],
    },
  );
}

API Reference

intersectionValidator

Validates that two form controls do not have overlapping values. Both controls must return arrays.

Signature

intersectionValidator(controlName1: string, controlName2: string): ValidatorFn

Parameters

  • controlName1: string

    The name of the first form control to compare.

  • controlName2: string

    The name of the second form control to compare.

Returns

A validator function that returns {intersection: true} if the arrays overlap, or null if they don't. You can check for this error in the form by accessing form.errors?.['intersection'].