ExecutionContextInterface
in
The context of a validation run.
The context collects all violations generated during the validation. By default, validators execute all validations in a new context:
$violations = $validator->validate($object);
When you make another call to the validator, while the validation is in progress, the violations will be isolated from each other:
public function validate(mixed $value, Constraint $constraint)
{
$validator = $this->context->getValidator();
// The violations are not added to $this->context
$violations = $validator->validate($value);
}
However, if you want to add the violations to the current context, use the method:
public function validate(mixed $value, Constraint $constraint)
{
$validator = $this->context->getValidator();
// The violations are added to $this->context
$validator
->inContext($this->context)
->validate($value)
;
}
Additionally, the context provides information about the current state of the validator, such as the currently validated class, the name of the currently validated property and more. These values change over time, so you cannot store a context and expect that the methods still return the same results later on.
Tags
Table of Contents
Methods
- addViolation() : mixed
- Adds a violation at the current node of the validation graph.
- buildViolation() : ConstraintViolationBuilderInterface
- Returns a builder for adding a violation with extended information.
- getClassName() : string|null
- Returns the class name of the current node.
- getGroup() : string|null
- Returns the validation group that is currently being validated.
- getMetadata() : MetadataInterface|null
- Returns the metadata for the currently validated value.
- getObject() : object|null
- Returns the currently validated object.
- getPropertyName() : string|null
- Returns the property name of the current node.
- getPropertyPath() : string
- Returns the property path to the value that the validator is currently validating.
- getRoot() : mixed
- Returns the value at which validation was started in the object graph.
- getValidator() : ValidatorInterface
- Returns the validator.
- getValue() : mixed
- Returns the value that the validator is currently validating.
- getViolations() : ConstraintViolationListInterface
- Returns the violations generated by the validator so far.
Methods
addViolation()
Adds a violation at the current node of the validation graph.
public
addViolation(string|Stringable $message[, array<string|int, mixed> $params = [] ]) : mixed
Parameters
- $message : string|Stringable
-
The error message as a string or a stringable object
- $params : array<string|int, mixed> = []
-
The parameters substituted in the error message
buildViolation()
Returns a builder for adding a violation with extended information.
public
buildViolation(string|Stringable $message[, array<string|int, mixed> $parameters = [] ]) : ConstraintViolationBuilderInterface
Call to add the violation when you're done with the configuration:
$context->buildViolation('Please enter a number between %min% and %max%.')
->setParameter('%min%', '3')
->setParameter('%max%', '10')
->setTranslationDomain('number_validation')
->addViolation();
Parameters
- $message : string|Stringable
-
The error message as a string or a stringable object
- $parameters : array<string|int, mixed> = []
-
The parameters substituted in the error message
Return values
ConstraintViolationBuilderInterfacegetClassName()
Returns the class name of the current node.
public
getClassName() : string|null
If the metadata of the current node does not implement or if no metadata is available for the current node, this method returns null.
Return values
string|nullgetGroup()
Returns the validation group that is currently being validated.
public
getGroup() : string|null
Return values
string|nullgetMetadata()
Returns the metadata for the currently validated value.
public
getMetadata() : MetadataInterface|null
With the core implementation, this method returns a instance if the current value is an object, a instance if the current value is the value of a property and a instance if the validated value is the result of a getter method.
If the validated value is neither of these, for example if the validator has been called with a plain value and constraint, this method returns null.
Return values
MetadataInterface|nullgetObject()
Returns the currently validated object.
public
getObject() : object|null
If the validator is currently validating a class constraint, the object of that class is returned. If it is validating a property or getter constraint, the object that the property/getter belongs to is returned.
In other cases, null is returned.
Return values
object|nullgetPropertyName()
Returns the property name of the current node.
public
getPropertyName() : string|null
If the metadata of the current node does not implement or if no metadata is available for the current node, this method returns null.
Return values
string|nullgetPropertyPath()
Returns the property path to the value that the validator is currently validating.
public
getPropertyPath([string $subPath = '' ]) : string
For example, take the following object graph:
(Person)---($address: Address)---($street: string)
When the Person instance is passed to the validator, the property path is initially empty. When the $address property of that person is validated, the property path is "address". When the $street property of the related Address instance is validated, the property path is "address.street".
Properties of objects are prefixed with a dot in the property path. Indices of arrays or objects implementing the interface are enclosed in brackets. For example, if the property in the previous example is $addresses and contains an array of Address instance, the property path generated for the $street property of one of these addresses is for example "addresses[0].street".
Parameters
- $subPath : string = ''
-
Optional. The suffix appended to the current property path.
Return values
string —The current property path. The result may be an empty string if the validator is currently validating the root value of the validation graph.
getRoot()
Returns the value at which validation was started in the object graph.
public
getRoot() : mixed
The validator, when given an object, traverses the properties and related objects and their properties. The root of the validation is the object from which the traversal started.
getValidator()
Returns the validator.
public
getValidator() : ValidatorInterface
Useful if you want to validate additional constraints:
public function validate(mixed $value, Constraint $constraint) { $validator = $this->context->getValidator();
$violations = $validator->validate($value, new Length(['min' => 3]));
if (count($violations) > 0) {
// ...
}
}
Return values
ValidatorInterfacegetValue()
Returns the value that the validator is currently validating.
public
getValue() : mixed
If you want to retrieve the object that was originally passed to the validator, use .
getViolations()
Returns the violations generated by the validator so far.
public
getViolations() : ConstraintViolationListInterface