ConstraintValidatorContext class final

The help a constraint gets while it checks one value: the validator's clock, and a way to report violations other than the default one.

The validator creates one per check and passes it to Constraint.isValidWithContext. Only custom constraints that override that method use it, mostly to put a class-level violation on a property or to report several problems with their own messages. Jakarta's ConstraintValidatorContext.

// A class-level constraint reporting on the `confirm` property.
context
  ..disableDefaultConstraintViolation()
  ..buildConstraintViolationWithTemplate('{passwords.mismatch}')
      .addPropertyNode('confirm')
      .addConstraintViolation();

Spring (Java):

context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("{passwords.mismatch}")
    .addPropertyNode("confirm")
    .addConstraintViolation();

Constructors

ConstraintValidatorContext({required String defaultConstraintMessageTemplate, ClockProvider clockProvider = const DefaultClockProvider()})
A context for a constraint whose message template is defaultConstraintMessageTemplate; clockProvider gives the time. The validator creates it; build one yourself only to test a constraint.

Properties

clockProvider → ClockProvider
The validator's clock; date constraints read "now" from it.
final
customViolations → List<({String messageTemplate, List<String> nodes})>
The violations added with buildConstraintViolationWithTemplate, in order: their message template and property nodes. The validator reads them after the check.
no setter
defaultConstraintMessageTemplate → String
The constraint's message before interpolation: its message, else its default. Pass it to buildConstraintViolationWithTemplate to move the usual message to another property.
final
defaultConstraintViolationDisabled → bool
Whether disableDefaultConstraintViolation was called; the validator reads it after the check.
no setter
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

buildConstraintViolationWithTemplate(String messageTemplate) → ConstraintViolationBuilder
Starts an extra violation with messageTemplate, interpolated like a constraint message ({key}, {attribute}). Finish it with ConstraintViolationBuilder.addConstraintViolation, or it is not reported.
disableDefaultConstraintViolation() → void
Drops the violation the validator would report when the constraint fails, so only the ones built with buildConstraintViolationWithTemplate are reported.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited