Skip to main content
Version: 0.6.0

prohibitedIf

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

Signature

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

// 'displayName' must be empty when 'isAnonymous' is truthy
new FormGroup({
isAnonymous: new FormControl(false),
displayName: new FormControl('', [
NguardValidators.CrossField.prohibitedIf('isAnonymous'),
]),
});

// 'inviteCode' is forbidden when 'role' is exactly 'admin'
new FormGroup({
role: new FormControl(''),
inviteCode: new FormControl('', [
NguardValidators.CrossField.prohibitedIf('role', 'admin'),
]),
});

Template-driven forms

<!-- Shorthand: prohibited if 'isAnonymous' is truthy -->
<input ngModel name="displayName" [nguardProhibitedIf]="'isAnonymous'" />

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

Error key

{ prohibitedIf: true }

Notes

  • "Empty" means the value is falsy (empty string, 0, false, null, undefined).
  • When the trigger value is omitted, any truthy sibling value triggers the prohibition.
  • When the control has no parent form group the rule never triggers and any value is accepted.

See also