Skip to main content
Version: 0.7.0

presentIf

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

Signature

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

Reactive forms

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

// 'taxId' must have a value (even empty string) when 'isBusiness' is truthy
new FormGroup({
isBusiness: new FormControl(false),
taxId: new FormControl(null, [
NguardValidators.CrossField.presentIf('isBusiness'),
]),
});

// 'state' must be present when 'country' is exactly 'US'
new FormGroup({
country: new FormControl(''),
state: new FormControl(null, [
NguardValidators.CrossField.presentIf('country', 'US'),
]),
});

Template-driven forms

<!-- Shorthand: present if 'isBusiness' is truthy -->
<input ngModel name="taxId" [nguardPresentIf]="'isBusiness'" />

<!-- Object form: present if sibling equals a specific value -->
<input
ngModel
name="state"
[nguardPresentIf]="{ fieldKey: 'country', value: 'US' }"
/>

Error key

{ presentIf: true }

Notes

  • "Present" means the value is anything other than null or undefined. Empty string, 0 and false all count as present — use requiredIf when you need a truthy value.
  • When the trigger value is omitted, any truthy sibling value triggers the rule.
  • When the control has no parent form group the rule never triggers and the field can be null.

See also