4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Context;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
16 use Symfony\Component\Validator\Mapping\MetadataInterface;
17 use Symfony\Component\Validator\Validator\ValidatorInterface;
18 use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
21 * The context of a validation run.
23 * The context collects all violations generated during the validation. By
24 * default, validators execute all validations in a new context:
26 * $violations = $validator->validate($object);
28 * When you make another call to the validator, while the validation is in
29 * progress, the violations will be isolated from each other:
31 * public function validate($value, Constraint $constraint)
33 * $validator = $this->context->getValidator();
35 * // The violations are not added to $this->context
36 * $violations = $validator->validate($value);
39 * However, if you want to add the violations to the current context, use the
40 * {@link ValidatorInterface::inContext()} method:
42 * public function validate($value, Constraint $constraint)
44 * $validator = $this->context->getValidator();
46 * // The violations are added to $this->context
48 * ->inContext($this->context)
53 * Additionally, the context provides information about the current state of
54 * the validator, such as the currently validated class, the name of the
55 * currently validated property and more. These values change over time, so you
56 * cannot store a context and expect that the methods still return the same
59 * @author Bernhard Schussek <bschussek@gmail.com>
61 interface ExecutionContextInterface extends LegacyExecutionContextInterface
64 * Returns a builder for adding a violation with extended information.
66 * Call {@link ConstraintViolationBuilderInterface::addViolation()} to
67 * add the violation when you're done with the configuration:
69 * $context->buildViolation('Please enter a number between %min% and %max%.')
70 * ->setParameter('%min%', 3)
71 * ->setParameter('%max%', 10)
72 * ->setTranslationDomain('number_validation')
75 * @param string $message The error message
76 * @param array $parameters The parameters substituted in the error message
78 * @return ConstraintViolationBuilderInterface The violation builder
80 public function buildViolation($message, array $parameters = array());
83 * Returns the validator.
85 * Useful if you want to validate additional constraints:
87 * public function validate($value, Constraint $constraint)
89 * $validator = $this->context->getValidator();
91 * $violations = $validator->validateValue($value, new Length(array('min' => 3)));
93 * if (count($violations) > 0) {
98 * @return ValidatorInterface
100 public function getValidator();
103 * Returns the currently validated object.
105 * If the validator is currently validating a class constraint, the
106 * object of that class is returned. If it is a validating a property or
107 * getter constraint, the object that the property/getter belongs to is
110 * In other cases, null is returned.
112 * @return object|null The currently validated object or null
114 public function getObject();
117 * Sets the currently validated value.
119 * @param mixed $value The validated value
120 * @param object|null $object The currently validated object
121 * @param MetadataInterface|null $metadata The validation metadata
122 * @param string $propertyPath The property path to the current value
124 * @internal Used by the validator engine. Should not be called by user
127 public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath);
130 * Sets the currently validated group.
132 * @param string|null $group The validated group
134 * @internal Used by the validator engine. Should not be called by user
137 public function setGroup($group);
140 * Sets the currently validated constraint.
142 * @param Constraint $constraint The validated constraint
144 * @internal Used by the validator engine. Should not be called by user
147 public function setConstraint(Constraint $constraint);
150 * Marks an object as validated in a specific validation group.
152 * @param string $cacheKey The hash of the object
153 * @param string $groupHash The group's name or hash, if it is group
156 * @internal Used by the validator engine. Should not be called by user
159 public function markGroupAsValidated($cacheKey, $groupHash);
162 * Returns whether an object was validated in a specific validation group.
164 * @param string $cacheKey The hash of the object
165 * @param string $groupHash The group's name or hash, if it is group
168 * @return bool Whether the object was already validated for that
171 * @internal Used by the validator engine. Should not be called by user
174 public function isGroupValidated($cacheKey, $groupHash);
177 * Marks a constraint as validated for an object.
179 * @param string $cacheKey The hash of the object
180 * @param string $constraintHash The hash of the constraint
182 * @internal Used by the validator engine. Should not be called by user
185 public function markConstraintAsValidated($cacheKey, $constraintHash);
188 * Returns whether a constraint was validated for an object.
190 * @param string $cacheKey The hash of the object
191 * @param string $constraintHash The hash of the constraint
193 * @return bool Whether the constraint was already validated
195 * @internal Used by the validator engine. Should not be called by user
198 public function isConstraintValidated($cacheKey, $constraintHash);
201 * Marks that an object was initialized.
203 * @param string $cacheKey The hash of the object
205 * @internal Used by the validator engine. Should not be called by user
208 * @see ObjectInitializerInterface
210 public function markObjectAsInitialized($cacheKey);
213 * Returns whether an object was initialized.
215 * @param string $cacheKey The hash of the object
217 * @return bool Whether the object was already initialized
219 * @internal Used by the validator engine. Should not be called by user
222 * @see ObjectInitializerInterface
224 public function isObjectInitialized($cacheKey);