Skip to content

Commit 01a222b

Browse files
bug symfony#49484 [Validator] Fix translation of AtLeastOneOf constraint message (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [Validator] Fix translation of AtLeastOneOf constraint message | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#41317 | License | MIT | Doc PR | _NA_ New try after Nicolas' hint on symfony#41325. This is a much simpler solution using execution context to get the base message of the constraint translated. ~I tried to create the relevant test case, but other locales doesn't seem to be available in validators test cases.~ I'm having a look at a test case in a dummy locale after symfony#49484 (comment) When using another locale, the base message of the constraint is actually translated now: <img width="1238" alt="image" src="https://user-images.githubusercontent.com/2144837/220455120-c17e7ac6-81c5-4dd0-a2fc-b100a6a00688.png"> Commits ------- a687c9a [Validator] Fix translation of AtLeastOneOf constraint message
2 parents 0955438 + a687c9a commit 01a222b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Symfony/Component/Validator/Constraints/AtLeastOneOfValidator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function validate($value, Constraint $constraint)
3131

3232
$validator = $this->context->getValidator();
3333

34-
$messages = [$constraint->message];
34+
// Build a first violation to have the base message of the constraint translated
35+
$baseMessageContext = clone $this->context;
36+
$baseMessageContext->buildViolation($constraint->message)->addViolation();
37+
$baseViolations = $baseMessageContext->getViolations();
38+
$messages = [(string) $baseViolations->get(\count($baseViolations) - 1)->getMessage()];
3539

3640
foreach ($constraint->constraints as $key => $item) {
3741
if (!\in_array($this->context->getGroup(), $item->groups, true)) {

src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Validator\Constraints\GreaterThan;
2323
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
2424
use Symfony\Component\Validator\Constraints\IdenticalTo;
25+
use Symfony\Component\Validator\Constraints\IsNull;
2526
use Symfony\Component\Validator\Constraints\Language;
2627
use Symfony\Component\Validator\Constraints\Length;
2728
use Symfony\Component\Validator\Constraints\LessThan;
@@ -37,6 +38,9 @@
3738
use Symfony\Component\Validator\Mapping\MetadataInterface;
3839
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
3940
use Symfony\Component\Validator\Validation;
41+
use Symfony\Contracts\Translation\LocaleAwareInterface;
42+
use Symfony\Contracts\Translation\TranslatorInterface;
43+
use Symfony\Contracts\Translation\TranslatorTrait;
4044

4145
/**
4246
* @author Przemysław Bogusz <[email protected]>
@@ -258,6 +262,40 @@ public function testNestedConstraintsAreNotExecutedWhenGroupDoesNotMatch()
258262

259263
$this->assertCount(1, $violations);
260264
}
265+
266+
public function testTranslatorIsCalledOnConstraintBaseMessageAndViolations()
267+
{
268+
$translator = new class() implements TranslatorInterface, LocaleAwareInterface {
269+
use TranslatorTrait;
270+
271+
public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string
272+
{
273+
if ('This value should satisfy at least one of the following constraints:' === $id) {
274+
return 'Dummy translation:';
275+
}
276+
277+
if ('This value should be null.' === $id) {
278+
return 'Dummy violation.';
279+
}
280+
281+
return $id;
282+
}
283+
};
284+
285+
$validator = Validation::createValidatorBuilder()
286+
->setTranslator($translator)
287+
->getValidator()
288+
;
289+
290+
$violations = $validator->validate('Test', [
291+
new AtLeastOneOf([
292+
new IsNull(),
293+
]),
294+
]);
295+
296+
$this->assertCount(1, $violations);
297+
$this->assertSame('Dummy translation: [1] Dummy violation.', $violations->get(0)->getMessage());
298+
}
261299
}
262300

263301
class ExpressionConstraintNested

0 commit comments

Comments
 (0)