8000 [Validator] Fix translation of AtLeastOneOf constraint message · symfony/symfony@a687c9a · GitHub
[go: up one dir, main page]

Skip to content

Commit a687c9a

Browse files
[Validator] Fix translation of AtLeastOneOf constraint message
1 parent 069b83c commit a687c9a

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 <przemyslaw.bogusz@tubotax.pl>
@@ -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)
0