Skip to content

[Validator] Incorrect signature for ConstraintViolationBuilderInterface::setParameter #58948

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

Open
Mika56 opened this issue Nov 20, 2024 · 2 comments

Comments

@Mika56
Copy link

Mika56 commented Nov 20, 2024

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

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@Mika56
Copy link
Author

Mika56 commented May 21, 2025

Still relevant

@carsonbot carsonbot removed the Stalled label May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants