2014-02-17 13:12:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Validator\Context;
|
|
|
|
|
2014-02-20 20:06:05 +00:00
|
|
|
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
|
2014-02-17 13:12:00 +00:00
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
2014-02-18 15:18:15 +00:00
|
|
|
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
|
2014-02-17 13:12:00 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-18 15:18:15 +00:00
|
|
|
* 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:
|
|
|
|
*
|
2014-02-20 20:27:30 +00:00
|
|
|
* $violations = $validator->validate($object);
|
2014-02-18 15:18:15 +00:00
|
|
|
*
|
|
|
|
* 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($value, Constraint $constraint)
|
|
|
|
* {
|
|
|
|
* $validator = $this->context->getValidator();
|
|
|
|
*
|
|
|
|
* // The violations are not added to $this->context
|
2014-02-20 20:27:30 +00:00
|
|
|
* $violations = $validator->validate($value);
|
2014-02-18 15:18:15 +00:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* However, if you want to add the violations to the current context, use the
|
|
|
|
* {@link ValidatorInterface::inContext()} method:
|
|
|
|
*
|
|
|
|
* public function validate($value, Constraint $constraint)
|
|
|
|
* {
|
|
|
|
* $validator = $this->context->getValidator();
|
|
|
|
*
|
|
|
|
* // The violations are added to $this->context
|
|
|
|
* $validator
|
|
|
|
* ->inContext($this->context)
|
2014-02-20 20:27:30 +00:00
|
|
|
* ->validate($value)
|
2014-02-18 15:18:15 +00:00
|
|
|
* ;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @since 2.5
|
2014-02-17 13:12:00 +00:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
2014-02-20 20:06:05 +00:00
|
|
|
interface ExecutionContextInterface extends LegacyExecutionContextInterface
|
2014-02-17 13:12:00 +00:00
|
|
|
{
|
|
|
|
/**
|
2014-02-18 15:18:15 +00:00
|
|
|
* Returns a builder for adding a violation with extended information.
|
|
|
|
*
|
|
|
|
* Call {@link ConstraintViolationBuilderInterface::addViolation()} to
|
|
|
|
* add the violation when you're done with the configuration:
|
2014-02-17 13:12:00 +00:00
|
|
|
*
|
2014-02-18 15:18:15 +00:00
|
|
|
* $context->buildViolation('Please enter a number between %min% and %max.')
|
|
|
|
* ->setParameter('%min%', 3)
|
|
|
|
* ->setParameter('%max%', 10)
|
|
|
|
* ->setTranslationDomain('number_validation')
|
|
|
|
* ->addViolation();
|
2014-02-17 13:12:00 +00:00
|
|
|
*
|
2014-02-18 15:18:15 +00:00
|
|
|
* @param string $message The error message
|
|
|
|
* @param array $parameters The parameters substituted in the error message
|
|
|
|
*
|
|
|
|
* @return ConstraintViolationBuilderInterface The violation builder
|
2014-02-17 13:12:00 +00:00
|
|
|
*/
|
2014-02-18 15:18:15 +00:00
|
|
|
public function buildViolation($message, array $parameters = array());
|
2014-02-17 13:12:00 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-18 15:18:15 +00:00
|
|
|
* Returns the validator.
|
2014-02-17 13:12:00 +00:00
|
|
|
*
|
2014-02-18 15:18:15 +00:00
|
|
|
* Useful if you want to validate additional constraints:
|
2014-02-17 13:12:00 +00:00
|
|
|
*
|
2014-02-18 15:18:15 +00:00
|
|
|
* public function validate($value, Constraint $constraint)
|
|
|
|
* {
|
|
|
|
* $validator = $this->context->getValidator();
|
|
|
|
*
|
|
|
|
* $violations = $validator->validateValue($value, new Length(array('min' => 3)));
|
|
|
|
*
|
|
|
|
* if (count($violations) > 0) {
|
|
|
|
* // ...
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @return ValidatorInterface
|
2014-02-17 13:12:00 +00:00
|
|
|
*/
|
2014-02-18 15:18:15 +00:00
|
|
|
public function getValidator();
|
2014-02-17 13:12:00 +00:00
|
|
|
}
|