Skip to content

[Validator] [UniqueValidator] Use correct variable as parameter in (custom) error message #57213

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
Jun 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function validate($value, Constraint $constraint)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public static function getValidValues()
/**
* @dataProvider getInvalidValues
*/
public function testInvalidValues($value)
public function testInvalidValues($value, $expectedMessageParam)
{
$constraint = new Unique([
'message' => 'myMessage',
]);
$this->validator->validate($value, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setParameter('{{ value }}', $expectedMessageParam)
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}
Expand All @@ -77,12 +77,12 @@ public static function getInvalidValues()
$object = new \stdClass();

return [
yield 'not unique booleans' => [[true, true]],
yield 'not unique integers' => [[1, 2, 3, 3]],
yield 'not unique floats' => [[0.1, 0.2, 0.1]],
yield 'not unique string' => [['a', 'b', 'a']],
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]]],
yield 'not unique objects' => [[$object, $object]],
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 @@ -95,7 +95,7 @@ public function testInvalidValueNamed()
$this->validator->validate([1, 2, 3, 3], $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setParameter('{{ value }}', '3')
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public function testExpectsInvalidNonStrictComparison()
]));

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setParameter('{{ value }}', '1')
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public function testExpectsInvalidCaseInsensitiveComparison()
]));

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setParameter('{{ value }}', '"hello"')
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}
Expand Down