Skip to content

[Validator] set the violation path only if the errorPath option is set #59060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Symfony/Component/Validator/Constraints/UniqueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ public function validate(mixed $value, Constraint $constraint): void
}

if (\in_array($element, $collectionElements, true)) {
$this->context->buildViolation($constraint->message)
->atPath("[$index]".(null !== $constraint->errorPath ? ".{$constraint->errorPath}" : ''))
$violationBuilder = $this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($element))
->setCode(Unique::IS_NOT_UNIQUE)
->addViolation();
->setCode(Unique::IS_NOT_UNIQUE);

if (null !== $constraint->errorPath) {
$violationBuilder->atPath("[$index].{$constraint->errorPath}");
}

$violationBuilder->addViolation();

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function getValidValues()
/**
* @dataProvider getInvalidValues
*/
public function testInvalidValues($value, $expectedMessageParam, string $expectedErrorPath)
public function testInvalidValues($value, $expectedMessageParam)
{
$constraint = new Unique([
'message' => 'myMessage',
Expand All @@ -71,7 +71,6 @@ public function testInvalidValues($value, $expectedMessageParam, string $expecte
$this->buildViolation('myMessage')
->setParameter('{{ value }}', $expectedMessageParam)
->setCode(Unique::IS_NOT_UNIQUE)
->atPath($expectedErrorPath)
->assertRaised();
}

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

return [
yield 'not unique booleans' => [[true, true], 'true', 'property.path[1]'],
yield 'not unique integers' => [[1, 2, 3, 3], 3, 'property.path[3]'],
yield 'not unique floats' => [[0.1, 0.2, 0.1], 0.1, 'property.path[2]'],
yield 'not unique string' => [['a', 'b', 'a'], '"a"', 'property.path[2]'],
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]], 'array', 'property.path[2]'],
yield 'not unique objects' => [[$object, $object], 'object', 'property.path[1]'],
yield 'not unique booleans' => [[true, true], 'true'],
yield 'not unique integers' => [[1, 2, 3, 3], 3],
yield 'not unique floats' => [[0.1, 0.2, 0.1], 0.1],
yield 'not unique string' => [['a', 'b', 'a'], '"a"'],
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]], 'array'],
yield 'not unique objects' => [[$object, $object], 'object'],
];
}

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

Expand Down Expand Up @@ -154,7 +152,6 @@ public function testExpectsNonUniqueObjects($callback)
$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setCode(Unique::IS_NOT_UNIQUE)
->atPath('property.path[2]')
->assertRaised();
}

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

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

Expand Down Expand Up @@ -251,7 +246,7 @@ public static function getInvalidFieldNames(): array
/**
* @dataProvider getInvalidCollectionValues
*/
public function testInvalidCollectionValues(array $value, array $fields, string $expectedMessageParam, string $expectedErrorPath)
public function testInvalidCollectionValues(array $value, array $fields, string $expectedMessageParam)
{
$this->validator->validate($value, new Unique([
'message' => 'myMessage',
Expand All @@ -260,7 +255,6 @@ public function testInvalidCollectionValues(array $value, array $fields, string
$this->buildViolation('myMessage')
->setParameter('{{ value }}', $expectedMessageParam)
->setCode(Unique::IS_NOT_UNIQUE)
->atPath($expectedErrorPath)
->assertRaised();
}

Expand All @@ -270,27 +264,25 @@ public static function getInvalidCollectionValues(): array
'unique string' => [[
['lang' => 'eng', 'translation' => 'hi'],
['lang' => 'eng', 'translation' => 'hello'],
], ['lang'], 'array', 'property.path[1]'],
], ['lang'], 'array'],
'unique floats' => [[
['latitude' => 51.509865, 'longitude' => -0.118092, 'poi' => 'capital'],
['latitude' => 52.520008, 'longitude' => 13.404954],
['latitude' => 51.509865, 'longitude' => -0.118092],
], ['latitude', 'longitude'], 'array', 'property.path[2]'],
], ['latitude', 'longitude'], 'array'],
'unique int' => [[
['id' => 1, 'email' => 'bar@email.com'],
['id' => 1, 'email' => 'foo@email.com'],
], ['id'], 'array', 'property.path[1]'],
], ['id'], 'array'],
'unique null' => [
[null, null],
[],
'null',
'property.path[1]',
],
'unique field null' => [
[['nullField' => null], ['nullField' => null]],
['nullField'],
'array',
'property.path[1]',
],
];
}
Expand Down