From 345981f6b69595a9777e9730df8070ea4d08f99d Mon Sep 17 00:00:00 2001 From: Martin Hason Date: Thu, 14 Apr 2011 11:48:15 +0200 Subject: [PATCH 1/4] [Validator] added support for plural messages --- .../Validator/ConstraintViolation.php | 12 +++++++++++- .../Validator/Constraints/ChoiceValidator.php | 4 ++-- .../Constraints/MaxLengthValidator.php | 2 +- .../Constraints/MinLengthValidator.php | 2 +- .../Component/Validator/ExecutionContext.php | 18 ++++++++++++------ .../Constraints/ChoiceValidatorTest.php | 6 +++--- .../Constraints/MaxLengthValidatorTest.php | 2 +- .../Constraints/MinLengthValidatorTest.php | 2 +- 8 files changed, 32 insertions(+), 16 deletions(-) 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)); } From 7a6376eb297fb91aebffd53ea6f4b9248c5b79cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Thu, 2 Feb 2012 13:36:42 +0100 Subject: [PATCH 2/4] [Form] added support for error message pluralization --- .../Validator/DelegatingValidator.php | 6 +++-- src/Symfony/Component/Form/FormError.php | 26 ++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php b/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php index 8717eb20b3..e3832fe143 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php +++ b/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php @@ -61,7 +61,8 @@ class DelegatingValidator implements FormValidatorInterface $propertyPath = new PropertyPath($violation->getPropertyPath()); $template = $violation->getMessageTemplate(); $parameters = $violation->getMessageParameters(); - $error = new FormError($template, $parameters); + $pluralization = $violation->getMessagePluralization(); + $error = new FormError($template, $parameters, $pluralization); $child = $form; foreach ($propertyPath->getElements() as $element) { @@ -82,7 +83,8 @@ class DelegatingValidator implements FormValidatorInterface $propertyPath = $violation->getPropertyPath(); $template = $violation->getMessageTemplate(); $parameters = $violation->getMessageParameters(); - $error = new FormError($template, $parameters); + $pluralization = $violation->getMessagePluralization(); + $error = new FormError($template, $parameters, $pluralization); foreach ($mapping as $mappedPath => $child) { if (preg_match($mappedPath, $propertyPath)) { diff --git a/src/Symfony/Component/Form/FormError.php b/src/Symfony/Component/Form/FormError.php index 3a389cfd9b..bac75b5c51 100644 --- a/src/Symfony/Component/Form/FormError.php +++ b/src/Symfony/Component/Form/FormError.php @@ -30,6 +30,12 @@ class FormError */ protected $messageParameters; + /** + * The value for error message pluralization + * @var integer|null + */ + protected $messagePluralization; + /** * Constructor * @@ -37,14 +43,16 @@ class FormError * $messageTemplate. * @see Symfony\Component\Translation\Translator * - * @param string $messageTemplate The template for the error message - * @param array $messageParameters The parameters that should be - * substituted in the message template. + * @param string $messageTemplate The template for the error message + * @param array $messageParameters The parameters that should be + * substituted in the message template. + * @param integer $messagePluralization The value for error message pluralization */ - public function __construct($messageTemplate, array $messageParameters = array()) + public function __construct($messageTemplate, array $messageParameters = array(), $messagePluralization = null) { $this->messageTemplate = $messageTemplate; $this->messageParameters = $messageParameters; + $this->messagePluralization = $messagePluralization; } /** @@ -76,4 +84,14 @@ class FormError { return $this->messageParameters; } + + /** + * Returns the value for error message pluralization. + * + * @return integer|null + */ + public function getMessagePluralization() + { + return $this->messagePluralization; + } } From c0715f12355b13bb6d2edae4a713a18d24438a5a Mon Sep 17 00:00:00 2001 From: Martin Hason Date: Thu, 14 Apr 2011 12:23:27 +0200 Subject: [PATCH 3/4] [FrameworkBundle], [TwigBundle] added support for form error message pluralization --- .../views/Form/form_div_layout.html.twig | 8 +++++++- .../views/Form/field_errors.html.php | 20 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig index 2a4d83ede2..914546a9ab 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig @@ -250,7 +250,13 @@ {% if errors|length > 0 %}
    {% for error in errors %} -
  • {{ error.messageTemplate|trans(error.messageParameters, 'validators') }}
  • +
  • {{ + error.messagePluralization is null + ? + error.messageTemplate|trans(error.messageParameters, 'validators') + : + error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators') + }}
  • {% endfor %}
{% endif %} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php index 777554aa6b..339e3d0009 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php @@ -1,11 +1,21 @@
    -
  • trans( - $error->getMessageTemplate(), - $error->getMessageParameters(), - 'validators' - ) ?>
  • +
  • getMessagePluralization()) { + echo $view['translator']->trans( + $error->getMessageTemplate(), + $error->getMessageParameters(), + 'validators' + ); + } else { + echo $view['translator']->transChoice( + $error->getMessageTemplate(), + $error->getMessagePluralization(), + $error->getMessageParameters(), + 'validators' + ); + }?>
From f9a486edde3ec208b7ab18171b91c2fac0c7b82a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Thu, 2 Feb 2012 13:48:24 +0100 Subject: [PATCH 4/4] [Validator] Added support for pluralization of the SizeLengthValidator --- .../Component/Validator/Constraints/SizeLengthValidator.php | 6 +++--- .../Validator/Constraints/SizeLengthValidatorTest.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/SizeLengthValidator.php b/src/Symfony/Component/Validator/Constraints/SizeLengthValidator.php index 86de9b4d9e..100d728dc2 100644 --- a/src/Symfony/Component/Validator/Constraints/SizeLengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/SizeLengthValidator.php @@ -54,7 +54,7 @@ class SizeLengthValidator extends ConstraintValidator $this->context->addViolation($constraint->exactMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->max, - )); + ), null, (int) $constraint->max); return false; } @@ -63,7 +63,7 @@ class SizeLengthValidator extends ConstraintValidator $this->context->addViolation($constraint->maxMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->max, - )); + ), null, (int) $constraint->max); return false; } @@ -72,7 +72,7 @@ class SizeLengthValidator extends ConstraintValidator $this->context->addViolation($constraint->minMessage, array( '{{ value }}' => $value, '{{ limit }}' => $constraint->min, - )); + ), null, (int) $constraint->min); return false; } diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/SizeLengthValidatorTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/SizeLengthValidatorTest.php index 610bf91766..624f77dde8 100644 --- a/tests/Symfony/Tests/Component/Validator/Constraints/SizeLengthValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/Constraints/SizeLengthValidatorTest.php @@ -129,7 +129,7 @@ class SizeLengthValidatorTest extends \PHPUnit_Framework_TestCase ->with('myMessage', array( '{{ value }}' => '1234', '{{ limit }}' => 5, - )); + ), null, 5); $this->assertFalse($this->validator->isValid('1234', $constraint)); } @@ -147,7 +147,7 @@ class SizeLengthValidatorTest extends \PHPUnit_Framework_TestCase ->with('myMessage', array( '{{ value }}' => '12345678901', '{{ limit }}' => 10, - )); + ), null, 10); $this->assertFalse($this->validator->isValid('12345678901', $constraint)); } @@ -165,7 +165,7 @@ class SizeLengthValidatorTest extends \PHPUnit_Framework_TestCase ->with('myMessage', array( '{{ value }}' => '1234', '{{ limit }}' => 5, - )); + ), null, 5); $this->assertFalse($this->validator->isValid('1234', $constraint)); }