[Validator] Incorrect signature for ConstraintViolationBuilderInterface::setParameter
#58 948 ouverte le 20 nov. 2024
Métriques du dépôt
- Stars
- (31 047 étoiles)
- Métriques de merge PR
- (Merge moyen 17j 3h) (157 PRs mergées en 30 j)
Description
Symfony version(s) affected
7.2
Description
Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface::setParameter has the following signature:
<?php
public function setParameter(string $key, string $value): static;
$value is restricted to a string. However, at least when using ICU message format, objects can be set as a parameter's value: https://symfony.com/doc/current/reference/formats/message_format.html#date-and-time
This issue can be worked around by using setParameters(), which doesn't restrict the type
How to reproduce
Define a custom validation message in the validators domain (using ICU format):
test_message: "{name} is invalid on {date, date}"
Add a custom assertion to a dummy class (a callback works):
<?php
namespace App\Model;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class Dummy
{
#[Assert\Callback]
public function assertTestFails(ExecutionContextInterface $context)
{
$context->buildViolation('test_message')
->setParameter('name', 'Test')
->setParameter('date', new \DateTimeImmutable())
->addViolation()
;
}
#[Assert\Callback]
public function assertTestWorks(ExecutionContextInterface $context)
{
$context->buildViolation('test_message')
->setParameters([
'name' => 'Test',
'date' => new \DateTimeImmutable(),
])
->addViolation()
;
}
}
Then use Validator component to validate an instance of this class.
Possible Solution
Since Translation component does not restrict parameters type, $value should be typed as mixed
Additional Context
No response