Skip to main content
Version: 0.10.0

declinedIf

Conditional declined: the value must be one of the declined set only when a sibling field matches a trigger condition.

Signature

NguardValidators.Boolean.declinedIf(
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 (===)

When the condition is not met any value passes — the rule only kicks in when the sibling matches.

Reactive forms

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

// 'wantsMarketingEmails' must be declined when 'role' is 'guest'
new FormGroup({
role: new FormControl(''),
wantsMarketingEmails: new FormControl(true, [
NguardValidators.Boolean.declinedIf('role', 'guest'),
]),
});

Template-driven forms

<!-- Shorthand: must be declined when 'isReadOnlyMode' is truthy -->
<input
type="checkbox"
ngModel
name="allowEdits"
[nguardDeclinedIf]="'isReadOnlyMode'"
/>

<!-- Object form: must be declined when sibling equals a specific value -->
<input
type="checkbox"
ngModel
name="wantsMarketingEmails"
[nguardDeclinedIf]="{ fieldKey: 'role', value: 'guest' }"
/>

Error key

{ declinedIf: true }

Notes

  • The declined set matches Laravel exactly: false, 'false', 0, '0', 'no', 'off'. Case-sensitive.
  • 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 any value passes.

See also