Skip to content

Commit 82bb267

Browse files
committed
bug symfony#57213 [Validator] [UniqueValidator] Use correct variable as parameter in (custom) error message (seho-nl, Sebastien Hoek)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Validator] [UniqueValidator] Use correct variable as parameter in (custom) error message | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Unique constraint should use the `$element` (the element violating uniqueness) and not `$values` (the collection) to build the error message. When adding a custom message to the Constraint, for example `message: 'Duplicate element found with value {{ value }}'` It outputs: `Duplicate element found with value "array"` And the expected output should be: `Duplicate element found with value "main"` (where main is an example value in the collection). Commits ------- 8ef75c2 [Validator] [UniqueValidator] Use correct variable as parameter in (custom) error message
2 parents 4d0aeed + 8ef75c2 commit 82bb267

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/Symfony/Component/Validator/Constraints/UniqueValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function validate($value, Constraint $constraint)
4545

4646
if (\in_array($element, $collectionElements, true)) {
4747
$this->context->buildViolation($constraint->message)
48-
->setParameter('{{ value }}', $this->formatValue($value))
48+
->setParameter('{{ value }}', $this->formatValue($element))
4949
->setCode(Unique::IS_NOT_UNIQUE)
5050
->addViolation();
5151

src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ public static function getValidValues()
5959
/**
6060
* @dataProvider getInvalidValues
6161
*/
62-
public function testInvalidValues($value)
62+
public function testInvalidValues($value, $expectedMessageParam)
6363
{
6464
$constraint = new Unique([
6565
'message' => 'myMessage',
6666
]);
6767
$this->validator->validate($value, $constraint);
6868

6969
$this->buildViolation('myMessage')
70-
->setParameter('{{ value }}', 'array')
70+
->setParameter('{{ value }}', $expectedMessageParam)
7171
->setCode(Unique::IS_NOT_UNIQUE)
7272
->assertRaised();
7373
}
@@ -77,12 +77,12 @@ public static function getInvalidValues()
7777
$object = new \stdClass();
7878

7979
return [
80-
yield 'not unique booleans' => [[true, true]],
81-
yield 'not unique integers' => [[1, 2, 3, 3]],
82-
yield 'not unique floats' => [[0.1, 0.2, 0.1]],
83-
yield 'not unique string' => [['a', 'b', 'a']],
84-
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]]],
85-
yield 'not unique objects' => [[$object, $object]],
80+
yield 'not unique booleans' => [[true, true], 'true'],
81+
yield 'not unique integers' => [[1, 2, 3, 3], 3],
82+
yield 'not unique floats' => [[0.1, 0.2, 0.1], 0.1],
83+
yield 'not unique string' => [['a', 'b', 'a'], '"a"'],
84+
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]], 'array'],
85+
yield 'not unique objects' => [[$object, $object], 'object'],
8686
];
8787
}
8888

@@ -95,7 +95,7 @@ public function testInvalidValueNamed()
9595
$this->validator->validate([1, 2, 3, 3], $constraint);
9696

9797
$this->buildViolation('myMessage')
98-
->setParameter('{{ value }}', 'array')
98+
->setParameter('{{ value }}', '3')
9999
->setCode(Unique::IS_NOT_UNIQUE)
100100
->assertRaised();
101101
}
@@ -176,7 +176,7 @@ public function testExpectsInvalidNonStrictComparison()
176176
]));
177177

178178
$this->buildViolation('myMessage')
179-
->setParameter('{{ value }}', 'array')
179+
->setParameter('{{ value }}', '1')
180180
->setCode(Unique::IS_NOT_UNIQUE)
181181
->assertRaised();
182182
}
@@ -206,7 +206,7 @@ public function testExpectsInvalidCaseInsensitiveComparison()
206206
]));
207207

208208
$this->buildViolation('myMessage')
209-
->setParameter('{{ value }}', 'array')
209+
->setParameter('{{ value }}', '"hello"')
210210
->setCode(Unique::IS_NOT_UNIQUE)
211211
->assertRaised();
212212
}

0 commit comments

Comments
 (0)