Skip to main content
Version: 0.9.0

prohibitedUnless

The field's value must be empty (falsy) unless a sibling field matches a trigger condition.

Signature

NguardValidators.CrossField.prohibitedUnless(
fieldKey: string,
value?: primitive,
isStrict?: boolean,
): ValidatorFn
ParameterTypeDefaultDescription
fieldKeystringThe key of the sibling field whose value bypasses the prohibition
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';

// 'inviteCode' is allowed only when 'role' is 'admin' — empty otherwise
new FormGroup({
role: new FormControl(''),
inviteCode: new FormControl('', [
NguardValidators.CrossField.prohibitedUnless('role', 'admin'),
]),
});

Template-driven forms

<!-- Shorthand: empty unless 'isAdmin' is truthy -->
<input ngModel name="inviteCode" [nguardProhibitedUnless]="'isAdmin'" />

<!-- Object form: empty unless sibling equals a specific value -->
<input
ngModel
name="inviteCode"
[nguardProhibitedUnless]="{ fieldKey: 'role', value: 'admin' }"
/>

Error key

{ prohibitedUnless: true }

Notes

  • "Empty" means the value is falsy (empty string, 0, false, null, undefined).
  • When the trigger value is omitted, any truthy sibling value bypasses the prohibition.
  • When the control has no parent form group the rule applies and the field's value must be empty.

See also