Skip to main content
Version: 0.7.0

presentUnless

The field's value must be present (not null and not undefined) unless a sibling field matches a trigger condition. Empty string, 0 and false count as present.

Signature

NguardValidators.CrossField.presentUnless(
fieldKey: string,
value?: primitive,
isStrict?: boolean,
): ValidatorFn
ParameterTypeDefaultDescription
fieldKeystringThe key of the sibling field whose value bypasses the rule
valueprimitive (optional)undefinedIf provided, the sibling must equal this value to bypass the rule
isStrictbooleanfalseIf true, the equality check against value is strict (===)

Reactive forms

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

// 'phone' must have a value (even empty string) unless 'preferEmailOnly' is truthy
new FormGroup({
preferEmailOnly: new FormControl(false),
phone: new FormControl(null, [
NguardValidators.CrossField.presentUnless('preferEmailOnly'),
]),
});

Template-driven forms

<!-- Shorthand: present unless 'preferEmailOnly' is truthy -->
<input ngModel name="phone" [nguardPresentUnless]="'preferEmailOnly'" />

<!-- Object form: present unless sibling equals a specific value -->
<input
ngModel
name="phone"
[nguardPresentUnless]="{ fieldKey: 'contactMethod', value: 'email' }"
/>

Error key

{ presentUnless: true }

Notes

  • "Present" means the value is anything other than null or undefined. Empty string, 0 and false all count as present — use requiredUnless when you need a truthy value.
  • When the trigger value is omitted, any truthy sibling value bypasses the rule.
  • When the control has no parent form group the rule applies and the field's value must not be null / undefined.

See also