Skip to content

Fix for issue 11049 #11398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Symfony/Component/Validator/ConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Validator;

use Symfony\Component\Validator\Context\ExecutionContextInterface as NewExecutionContextInterface;

/**
* Base class for constraint validators
*
Expand All @@ -21,15 +23,18 @@
abstract class ConstraintValidator implements ConstraintValidatorInterface
{
/**
* @var ExecutionContextInterface
* @var ExecutionContextInterface|NewExecutionContextInterface
*/
protected $context;

/**
* {@inheritdoc}
*/
public function initialize(ExecutionContextInterface $context)
public function initialize($context)
{
if (!$context instanceof NewExecutionContextInterface && !$context instanceof ExecutionContextInterface) {
throw new \InvalidArgumentException('Context must be instance of Symfony\Component\Validator\Context\ExecutionContextInterface or Symfony\Component\Validator\ExecutionContextInterface');
}
$this->context = $context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Validator;

use Symfony\Component\Validator\Context\ExecutionContextInterface as NewExecutionContextInterface;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
Expand All @@ -21,9 +23,9 @@ interface ConstraintValidatorInterface
/**
* Initializes the constraint validator.
*
* @param ExecutionContextInterface $context The current validation context
* @param ExecutionContextInterface|NewExecutionContextInterface $context The current validation context
*/
public function initialize(ExecutionContextInterface $context);
public function initialize($context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, changing this interface is indeed a BC break.

@webmozart what would be the right way to handle the 2.5 API there ?


/**
* Checks if the passed value is valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\ExecutionContextInterface;

class ConstraintAValidator extends ConstraintValidator
{
public static $passedContext;

public function initialize(ExecutionContextInterface $context)
public function initialize($context)
{
parent::initialize($context);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* This file is part of the Symfony package.
*
* (c) Fabien Potiencier (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\Tests\Validator;

use Symfony\Component\Validator\Tests\Fixtures\ConstraintAValidator;

class ConstraintValidatorTest extends \PHPUnit_Framework_TestCase
{
public function testConstraintValidatorSinceSymfony25()
{
$context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface');
$constraintvalidator = new ConstraintAValidator();
$constraintvalidator->initialize($context);
$this->assertAttributeSame($context, 'context', $constraintvalidator);
}

public function testConstraintValidatorUntilSymfony24()
{
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
$constraintvalidator = new ConstraintAValidator();
$constraintvalidator->initialize($context);
$this->assertAttributeSame($context, 'context', $constraintvalidator);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidConstraintValidator()
{
$constraintvalidator = new ConstraintAValidator();
$constraintvalidator->initialize(null);
}
}