Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 345981f

Browse filesBrowse files
committed
[Validator] added support for plural messages
1 parent 1e3028d commit 345981f
Copy full SHA for 345981f

File tree

Expand file treeCollapse file tree

8 files changed

+32
-16
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+32
-16
lines changed

‎src/Symfony/Component/Validator/ConstraintViolation.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/ConstraintViolation.php
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ class ConstraintViolation
2020
{
2121
protected $messageTemplate;
2222
protected $messageParameters;
23+
protected $messagePluralization;
2324
protected $root;
2425
protected $propertyPath;
2526
protected $invalidValue;
2627

27-
public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue)
28+
public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null)
2829
{
2930
$this->messageTemplate = $messageTemplate;
3031
$this->messageParameters = $messageParameters;
32+
$this->messagePluralization = $messagePluralization;
3133
$this->root = $root;
3234
$this->propertyPath = $propertyPath;
3335
$this->invalidValue = $invalidValue;
@@ -68,6 +70,14 @@ public function getMessageParameters()
6870
return $this->messageParameters;
6971
}
7072

73+
/**
74+
* @return integer|null
75+
*/
76+
public function getMessagePluralization()
77+
{
78+
return $this->messagePluralization;
79+
}
80+
7181
/**
7282
* Returns the violation message.
7383
*

‎src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/ChoiceValidator.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ public function isValid($value, Constraint $constraint)
7575
$count = count($value);
7676

7777
if ($constraint->min !== null && $count < $constraint->min) {
78-
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min));
78+
$this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
7979

8080
return false;
8181
}
8282

8383
if ($constraint->max !== null && $count > $constraint->max) {
84-
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max));
84+
$this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
8585

8686
return false;
8787
}

‎src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/MaxLengthValidator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function isValid($value, Constraint $constraint)
5454
$this->context->addViolation($constraint->message, array(
5555
'{{ value }}' => $value,
5656
'{{ limit }}' => $constraint->limit,
57-
));
57+
), null, (int) $constraint->limit);
5858

5959
return false;
6060
}

‎src/Symfony/Component/Validator/Constraints/MinLengthValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/MinLengthValidator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function isValid($value, Constraint $constraint)
5454
$this->context->addViolation($constraint->message, array(
5555
'{{ value }}' => $value,
5656
'{{ limit }}' => $constraint->limit,
57-
));
57+
), null, (int) $constraint->limit);
5858

5959
return false;
6060
}

‎src/Symfony/Component/Validator/ExecutionContext.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/ExecutionContext.php
+12-6Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,20 @@ public function __clone()
5656
* @param string $message The error message.
5757
* @param array $params The parameters parsed into the error message.
5858
* @param mixed $invalidValue The invalid, validated value.
59+
* @param integer|null $pluralization The number to use to pluralize of the message.
5960
*
6061
* @api
6162
*/
62-
public function addViolation($message, array $params = array(), $invalidValue = null)
63+
public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null)
6364
{
6465
$this->globalContext->addViolation(new ConstraintViolation(
6566
$message,
6667
$params,
6768
$this->globalContext->getRoot(),
6869
$this->propertyPath,
6970
// check using func_num_args() to allow passing null values
70-
func_num_args() === 3 ? $invalidValue : $this->value
71+
func_num_args() === 3 ? $invalidValue : $this->value,
72+
$pluralization
7173
));
7274
}
7375

@@ -79,16 +81,18 @@ public function addViolation($message, array $params = array(), $invalidValue =
7981
* @param string $message The error message.
8082
* @param array $params The parameters parsed into the error message.
8183
* @param mixed $invalidValue The invalid, validated value.
84+
* @param integer|null $pluralization The number to use to pluralize of the message.
8285
*/
83-
public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null)
86+
public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null, $pluralization = null)
8487
{
8588
$this->globalContext->addViolation(new ConstraintViolation(
8689
$message,
8790
$params,
8891
$this->globalContext->getRoot(),
8992
$propertyPath,
9093
// check using func_num_args() to allow passing null values
91-
func_num_args() === 4 ? $invalidValue : $this->value
94+
func_num_args() === 4 ? $invalidValue : $this->value,
95+
$pluralization
9296
));
9397
}
9498

@@ -100,16 +104,18 @@ public function addViolationAtPath($propertyPath, $message, array $params = arra
100104
* @param string $message The error message.
101105
* @param array $params The parameters parsed into the error message.
102106
* @param mixed $invalidValue The invalid, validated value.
107+
* @param integer|null $pluralization The number to use to pluralize of the message.
103108
*/
104-
public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null)
109+
public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null)
105110
{
106111
$this->globalContext->addViolation(new ConstraintViolation(
107112
$message,
108113
$params,
109114
$this->globalContext->getRoot(),
110115
$this->getPropertyPath($subPath),
111116
// check using func_num_args() to allow passing null values
112-
func_num_args() === 4 ? $invalidValue : $this->value
117+
func_num_args() === 4 ? $invalidValue : $this->value,
118+
$pluralization
113119
));
114120
}
115121

‎tests/Symfony/Tests/Component/Validator/Constraints/ChoiceValidatorTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/Validator/Constraints/ChoiceValidatorTest.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function testInvalidChoice()
161161
->method('addViolation')
162162
->with('myMessage', array(
163163
'{{ value }}' => 'baz',
164-
));
164+
), null, null);
165165

166166
$this->assertFalse($this->validator->isValid('baz', $constraint));
167167
}
@@ -196,7 +196,7 @@ public function testTooFewChoices()
196196
->method('addViolation')
197197
->with('myMessage', array(
198198
'{{ limit }}' => 2,
199-
));
199+
), null, 2);
200200

201201
$this->assertFalse($this->validator->isValid(array('foo'), $constraint));
202202
}
@@ -214,7 +214,7 @@ public function testTooManyChoices()
214214
->method('addViolation')
215215
->with('myMessage', array(
216216
'{{ limit }}' => 2,
217-
));
217+
), null, 2);
218218

219219
$this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint));
220220
}

‎tests/Symfony/Tests/Component/Validator/Constraints/MaxLengthValidatorTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/Validator/Constraints/MaxLengthValidatorTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testInvalidValues($value, $mbOnly = false)
101101
->with('myMessage', array(
102102
'{{ value }}' => $value,
103103
'{{ limit }}' => 5,
104-
));
104+
), null, 5);
105105

106106
$this->assertFalse($this->validator->isValid($value, $constraint));
107107
}

‎tests/Symfony/Tests/Component/Validator/Constraints/MinLengthValidatorTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/Validator/Constraints/MinLengthValidatorTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testInvalidValues($value, $mbOnly = false)
101101
->with('myMessage', array(
102102
'{{ value }}' => $value,
103103
'{{ limit }}' => 5,
104-
));
104+
), null, 5);
105105

106106
$this->assertFalse($this->validator->isValid($value, $constraint));
107107
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.