8000 set the violation path only if the "errorPath" option is set · symfony/symfony@88cf73e · GitHub
[go: up one dir, main page]

Skip to content

Commit 88cf73e

Browse files
committed
set the violation path only if the "errorPath" option is set
1 parent 273fa3f commit 88cf73e

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ public function validate(mixed $value, Constraint $constraint): void
4747
}
4848

4949
if (\in_array($element, $collectionElements, true)) {
50-
$this->context->buildViolation($constraint->message)
51-
->atPath("[$index]".(null !== $constraint->errorPath ? ".{$constraint->errorPath}" : ''))
50+
$violationBuilder = $this->context->buildViolation($constraint->message)
5251
->setParameter('{{ value }}', $this->formatValue($element))
53-
->setCode(Unique::IS_NOT_UNIQUE)
54-
->addViolation();
52+
->setCode(Unique::IS_NOT_UNIQUE);
53+
54+
if (null !== $constraint->errorPath) {
55+
$violationBuilder->atPath("[$index].{$constraint->errorPath}");
56+
}
57+
58+
$violationBuilder->addViolation();
5559

5660
return;
5761
}

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function getValidValues()
6161
/**
6262
* @dataProvider getInvalidValues
6363
*/
64-
public function testInvalidValues($value, $expectedMessageParam, string $expectedErrorPath)
64+
public function testInvalidValues($value, $expectedMessageParam)
6565
{
6666
$constraint = new Unique([
6767
'message' => 'myMessage',
@@ -71,7 +71,6 @@ public function testInvalidValues($value, $expectedMessageParam, string $expecte
7171
$this->buildViolation('myMessage')
7272
->setParameter('{{ value }}', $expectedMessageParam)
7373
->setCode(Unique::IS_NOT_UNIQUE)
74-
->atPath($expectedErrorPath)
7574
->assertRaised();
7675
}
7776

@@ -80,12 +79,12 @@ public static function getInvalidValues()
8079
$object = new \stdClass();
8180

8281
return [
83-
yield 'not unique booleans' => [[true, true], 'true', 'property.path[1]'],
84-
yield 'not unique integers' => [[1, 2, 3, 3], 3, 'property.path[3]'],
85-
yield 'not unique floats' => [[0.1, 0.2, 0.1], 0.1, 'property.path[2]'],
86-
yield 'not unique string' => [['a', 'b', 'a'], '"a"', 'property.path[2]'],
87-
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]], 'array', 'property.path[2]'],
88-
yield 'not unique objects' => [[$object, $object], 'object', 'property.path[1]'],
82+
yield 'not unique booleans' => [[true, true], 'true'],
83+
yield 'not unique integers' => [[1, 2, 3, 3], 3],
84+
yield 'not unique floats' => [[0.1, 0.2, 0.1], 0.1],
85+
yield 'not unique string' => [['a', 'b', 'a'], '"a"'],
86+
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]], 'array'],
87+
yield 'not unique objects' => [[$object, $object], 'object'],
8988
];
9089
}
9190

@@ -97,7 +96,6 @@ public function testInvalidValueNamed()
9796
$this->buildViolation('myMessage')
9897
->setParameter('{{ value }}', '3')
9998
->setCode(Unique::IS_NOT_UNIQUE)
100-
->atPath('property.path[3]')
10199
->assertRaised();
102100
}
103101

@@ -154,7 +152,6 @@ public function testExpectsNonUniqueObjects($callback)
154152
$this->buildViolation('myMessage')
155153
->setParameter('{{ value }}', 'array')
156154
->setCode(Unique::IS_NOT_UNIQUE)
157-
->atPath('property.path[2]')
158155
->assertRaised();
159156
}
160157

@@ -179,7 +176,6 @@ public function testExpectsInvalidNonStrictComparison()
179176
$this->buildViolation('myMessage')
180177
->setParameter('{{ value }}', '1')
181178
->setCode(Unique::IS_NOT_UNIQUE)
182-
->atPath('property.path[1]')
183179
->assertRaised();
184180
}
185181

@@ -206,7 +202,6 @@ public function testExpectsInvalidCaseInsensitiveComparison()
206202
$this->buildViolation('myMessage')
207203
->setParameter('{{ value }}', '"hello"')
208204
->setCode(Unique::IS_NOT_UNIQUE)
209-
->atPath('property.path[1]')
210205
->assertRaised();
211206
}
212207

@@ -251,7 +246,7 @@ public static function getInvalidFieldNames(): array
251246
/**
252247
* @dataProvider getInvalidCollectionValues
253248
*/
254-
public function testInvalidCollectionValues(array $value, array $fields, string $expectedMessageParam, string $expectedErrorPath)
249+
public function testInvalidCollectionValues(array $value, array $fields, string $expectedMessageParam)
255250
{
256251
$this->validator->validate($value, new Unique([
257252
'message' => 'myMessage',
@@ -260,7 +255,6 @@ public function testInvalidCollectionValues(array $value, array $fields, string
260255
$this->buildViolation('myMessage')
261256
->setParameter('{{ value }}', $expectedMessageParam)
262257
->setCode(Unique::IS_NOT_UNIQUE)
263-
->atPath($expectedErrorPath)
264258
->assertRaised();
265259
}
266260

@@ -270,27 +264,25 @@ public static function getInvalidCollectionValues(): array
270264
'unique string' => [[
271265
['lang' => 'eng', 'translation' => 'hi'],
272266
['lang' => 'eng', 'translation' => 'hello'],
273-
], ['lang'], 'array', 'property.path[1]'],
267+
], ['lang'], 'array'],
274268
'unique floats' => [[
275269
['latitude' => 51.509865, 'longitude' => -0.118092, 'poi' => 'capital'],
276270
['latitude' => 52.520008, 'longitude' => 13.404954],
277271
['latitude' => 51.509865, 'longitude' => -0.118092],
278-
], ['latitude', 'longitude'], 'array', 'property.path[2]'],
272+
], ['latitude', 'longitude'], 'array'],
279273
'unique int' => [[
280274
['id' => 1, 'email' => 'bar@email.com'],
281275
['id' => 1, 'email' => 'foo@email.com'],
282-
], ['id'], 'array', 'property.path[1]'],
276+
], ['id'], 'array'],
283277
'unique null' => [
284278
[null, null],
285279
[],
286280
'null',
287-
'property.path[1]',
288281
],
289282
'unique field null' => [
290283
[['nullField' => null], ['nullField' => null]],
291284
['nullField'],
292285
'array',
293-
'property.path[1]',
294286
],
295287
];
296288
}

0 commit comments

Comments
 (0)
0