Skip to content

[Validation][Range] Message not properly translated #36133

@ktherage

Description

@ktherage

Symfony version(s) affected: 4.4.5

Description
When upgrading an application from 4.3 to 4.4, I've noticed that for the only Range constraint that I use the ConstraintViolation message which was the translated on 4.3 is now replaced by the default message.

How to reproduce
reproducer :

<?php
require_once __DIR__.'/vendor/autoload.php';

use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\ArrayLoader;

$translator = new Translator('en_GB');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', [
    'violation.minMessage' => 'Bad guy !',
    'violation.maxMessage' => 'really really really bad guy !',
], 'en_GB');

$rangeConstraint = new Constraints\Range([
	'min' => 0,
	'max' => 2,
	'minMessage' => 'violation.minMessage',
	'maxMessage' => 'violation.maxMessage'
]);
$validatorBuilder = Validation::createValidatorBuilder();
$validatorBuilder->setTranslator($translator);
$validator = $validatorBuilder->getValidator();

$violations = $validator->validate(-1, [
    $rangeConstraint
]);

foreach ($violations as $violation) {
	dump($violation->getMessage());	
}

$violations = $validator->validate(3, [
    $rangeConstraint
]);

foreach ($violations as $violation) {
	dump($violation->getMessage());	
}

composer.json :

{
	"require": {
		"symfony/validator" : "4.4.*",
		"symfony/translation": "4.4.*",
		"symfony/var-dumper": "4.4.*"
	}
}

this, displays the following :

"This value should be between 0 and 2."
"This value should be between 0 and 2."

Instead of displaying :

"Bad guy !"
"really really really bad guy !"

Possible Solution

No idea for the moment.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions