Skip to main content
Version: 0.10.0

requiredWithout

The field is required when any of the listed sibling fields is missing.

Signature

NguardValidators.CrossField.requiredWithout(...fieldKeys: string[]): ValidatorFn
ParameterTypeDescription
fieldKeys...stringOne or more sibling keys. The rule applies as soon as one of them is empty

Reactive forms

import { FormControl, FormGroup } from '@angular/forms';
import { NguardValidators } from 'ng-nguard';

// 'phone' is required when either 'email' or 'fax' is empty
new FormGroup({
email: new FormControl(''),
fax: new FormControl(''),
phone: new FormControl('', [
NguardValidators.CrossField.requiredWithout('email', 'fax'),
]),
});

Template-driven forms

<!-- Single sibling -->
<input ngModel name="phone" [nguardRequiredWithout]="'email'" />

<!-- Multiple siblings -->
<input
ngModel
name="phone"
[nguardRequiredWithout]="['email', 'fax']"
/>

Error key

{ requiredWithout: true }

Notes

  • "Missing" means the sibling's value is falsy (empty string, 0, null, undefined).
  • The rule triggers when any of the listed siblings is missing — for the all-must-be-missing variant see requiredWithoutAll.
  • When the control has no parent form group, every sibling is unreachable and treated as missing — the rule fires and the field's value must be truthy.

See also