diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index a069dabb17..a002f5b393 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -20,14 +20,16 @@ class ConstraintViolation { protected $messageTemplate; protected $messageParameters; + protected $messagePluralization; protected $root; protected $propertyPath; protected $invalidValue; - public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue) + public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null) { $this->messageTemplate = $messageTemplate; $this->messageParameters = $messageParameters; + $this->messagePluralization = $messagePluralization; $this->root = $root; $this->propertyPath = $propertyPath; $this->invalidValue = $invalidValue; @@ -68,6 +70,14 @@ class ConstraintViolation return $this->messageParameters; } + /** + * @return integer|null + */ + public function getMessagePluralization() + { + return $this->messagePluralization; + } + /** * Returns the violation message. * diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index c5401eb4fc..7dabfd7ca3 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -75,13 +75,13 @@ class ChoiceValidator extends ConstraintValidator $count = count($value); if ($constraint->min !== null && $count < $constraint->min) { - $this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min)); + $this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min); return false; } if ($constraint->max !== null && $count > $constraint->max) { - $this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max)); + $this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max); return false; } diff --git a/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php b/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php index 48bab5fde2..686c603f19 100644 --- a/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php @@ -54,7 +54,7 @@ class MaxLengthValidator extends ConstraintValidator $this->context->addViolation($constraint->message, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->limit, - )); + ), null, (int) $constraint->limit); return false; } diff --git a/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php b/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php index b148ec794a..6b3f13ab77 100644 --- a/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/MinLengthValidator.php @@ -54,7 +54,7 @@ class MinLengthValidator extends ConstraintValidator $this->context->addViolation($constraint->message, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->limit, - )); + ), null, (int) $constraint->limit); return false; } diff --git a/src/Symfony/Component/Validator/ExecutionContext.php b/src/Symfony/Component/Validator/ExecutionContext.php index a84e2e1811..9023efb83e 100644 --- a/src/Symfony/Component/Validator/ExecutionContext.php +++ b/src/Symfony/Component/Validator/ExecutionContext.php @@ -56,10 +56,11 @@ class ExecutionContext * @param string $message The error message. * @param array $params The parameters parsed into the error message. * @param mixed $invalidValue The invalid, validated value. + * @param integer|null $pluralization The number to use to pluralize of the message. * * @api */ - public function addViolation($message, array $params = array(), $invalidValue = null) + public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null) { $this->globalContext->addViolation(new ConstraintViolation( $message, @@ -67,7 +68,8 @@ class ExecutionContext $this->globalContext->getRoot(), $this->propertyPath, // check using func_num_args() to allow passing null values - func_num_args() === 3 ? $invalidValue : $this->value + func_num_args() === 3 ? $invalidValue : $this->value, + $pluralization )); } @@ -79,8 +81,9 @@ class ExecutionContext * @param string $message The error message. * @param array $params The parameters parsed into the error message. * @param mixed $invalidValue The invalid, validated value. + * @param integer|null $pluralization The number to use to pluralize of the message. */ - public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null) + public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null, $pluralization = null) { $this->globalContext->addViolation(new ConstraintViolation( $message, @@ -88,7 +91,8 @@ class ExecutionContext $this->globalContext->getRoot(), $propertyPath, // check using func_num_args() to allow passing null values - func_num_args() === 4 ? $invalidValue : $this->value + func_num_args() === 4 ? $invalidValue : $this->value, + $pluralization )); } @@ -100,8 +104,9 @@ class ExecutionContext * @param string $message The error message. * @param array $params The parameters parsed into the error message. * @param mixed $invalidValue The invalid, validated value. + * @param integer|null $pluralization The number to use to pluralize of the message. */ - public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null) + public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null) { $this->globalContext->addViolation(new ConstraintViolation( $message, @@ -109,7 +114,8 @@ class ExecutionContext $this->globalContext->getRoot(), $this->getPropertyPath($subPath), // check using func_num_args() to allow passing null values - func_num_args() === 4 ? $invalidValue : $this->value + func_num_args() === 4 ? $invalidValue : $this->value, + $pluralization )); } diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/ChoiceValidatorTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/ChoiceValidatorTest.php index 97d4dd3441..0c42afeed6 100644 --- a/tests/Symfony/Tests/Component/Validator/Constraints/ChoiceValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/Constraints/ChoiceValidatorTest.php @@ -161,7 +161,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase ->method('addViolation') ->with('myMessage', array( '{{ value }}' => 'baz', - )); + ), null, null); $this->assertFalse($this->validator->isValid('baz', $constraint)); } @@ -196,7 +196,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase ->method('addViolation') ->with('myMessage', array( '{{ limit }}' => 2, - )); + ), null, 2); $this->assertFalse($this->validator->isValid(array('foo'), $constraint)); } @@ -214,7 +214,7 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase ->method('addViolation') ->with('myMessage', array( '{{ limit }}' => 2, - )); + ), null, 2); $this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint)); } diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/MaxLengthValidatorTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/MaxLengthValidatorTest.php index 2be5b4fb6a..7d82ebcda5 100644 --- a/tests/Symfony/Tests/Component/Validator/Constraints/MaxLengthValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/Constraints/MaxLengthValidatorTest.php @@ -101,7 +101,7 @@ class MaxLengthValidatorTest extends \PHPUnit_Framework_TestCase ->with('myMessage', array( '{{ value }}' => $value, '{{ limit }}' => 5, - )); + ), null, 5); $this->assertFalse($this->validator->isValid($value, $constraint)); } diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/MinLengthValidatorTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/MinLengthValidatorTest.php index 43066704d8..4f5848aa3c 100644 --- a/tests/Symfony/Tests/Component/Validator/Constraints/MinLengthValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/Constraints/MinLengthValidatorTest.php @@ -101,7 +101,7 @@ class MinLengthValidatorTest extends \PHPUnit_Framework_TestCase ->with('myMessage', array( '{{ value }}' => $value, '{{ limit }}' => 5, - )); + ), null, 5); $this->assertFalse($this->validator->isValid($value, $constraint)); }