Skip to main content
Version: 0.7.0

requiredWithoutAll

The field is required only when every listed sibling field is missing.

Signature

NguardValidators.CrossField.requiredWithoutAll(...fieldKeys: string[]): ValidatorFn
ParameterTypeDescription
fieldKeys...stringOne or more sibling keys. The rule applies only when all of them are empty

Reactive forms

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

// 'fallbackContact' is required only when both 'email' and 'phone' are empty
new FormGroup({
email: new FormControl(''),
phone: new FormControl(''),
fallbackContact: new FormControl('', [
NguardValidators.CrossField.requiredWithoutAll('email', 'phone'),
]),
});

Template-driven forms

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

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

Error key

{ requiredWithoutAll: true }

Notes

  • "Missing" means the sibling's value is falsy (empty string, 0, null, undefined).
  • The rule triggers only when every listed sibling is missing — for the any-must-be-missing variant see requiredWithout.
  • 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