Skip to content

Commit 33e9a42

Browse files
committed
fix(validator): custom message was not translated
fixes #7336
1 parent 563cd63 commit 33e9a42

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

src/Validator/Util/ParameterValidationConstraints.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static function getParameterValidationConstraints(Parameter $parameter, ?
133133
}
134134

135135
if ($required && false !== $allowEmptyValue) {
136-
$assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
136+
$assertions[] = new NotNull();
137137
}
138138

139139
if (isset($schema['minItems']) || isset($schema['maxItems'])) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;
15+
16+
use ApiPlatform\Metadata\GetCollection;
17+
use ApiPlatform\Metadata\Operation;
18+
use ApiPlatform\Metadata\QueryParameter;
19+
use Symfony\Component\Validator\Constraints as Assert;
20+
21+
#[GetCollection(
22+
uriTemplate: '/translate_validation_error',
23+
parameters: [
24+
'name' => new QueryParameter(
25+
description: 'Nome della persona',
26+
constraints: [
27+
new Assert\NotBlank(),
28+
]
29+
),
30+
'surname' => new QueryParameter(
31+
description: 'Cognome della persona',
32+
required: true,
33+
),
34+
],
35+
provider: [self::class, 'provide']
36+
)]
37+
class TranslateValidationError
38+
{
39+
public static function provide(Operation $operation, array $uriVariables = []): array
40+
{
41+
return [];
42+
}
43+
}

tests/Fixtures/app/AppKernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
139139
'annotations' => false,
140140
'handle_all_throwables' => true,
141141
'uid' => ['default_uuid_version' => 7, 'time_based_uuid_version' => 7],
142+
'set_locale_from_accept_language' => true,
142143
];
143144
} else {
144145
$config = [
@@ -155,6 +156,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
155156
'router' => ['utf8' => true],
156157
'http_method_override' => false,
157158
'annotations' => false,
159+
'set_locale_from_accept_language' => true,
158160
];
159161
}
160162

tests/Functional/Parameters/ParameterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Tests\Functional\Parameters;
1515

1616
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\TranslateValidationError;
1718
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter;
1819
use ApiPlatform\Tests\SetupClassResourcesTrait;
1920
use PHPUnit\Framework\Attributes\DataProvider;
@@ -29,7 +30,7 @@ final class ParameterTest extends ApiTestCase
2930
*/
3031
public static function getResources(): array
3132
{
32-
return [WithParameter::class];
33+
return [WithParameter::class, TranslateValidationError::class];
3334
}
3435

3536
public function testWithGroupFilter(): void

tests/Functional/Parameters/ValidationTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace ApiPlatform\Tests\Functional\Parameters;
1515

1616
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17-
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ValidateParameterBeforeProvider;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\TranslateValidationError;
1818
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter;
1919
use ApiPlatform\Tests\SetupClassResourcesTrait;
2020

@@ -29,7 +29,7 @@ final class ValidationTest extends ApiTestCase
2929
*/
3030
public static function getResources(): array
3131
{
32-
return [WithParameter::class, ValidateParameterBeforeProvider::class];
32+
return [WithParameter::class, TranslateValidationError::class];
3333
}
3434

3535
public function testWithGroupFilter(): void
@@ -188,4 +188,14 @@ public function testValidatePattern(): void
188188
self::createClient()->request('GET', 'validate_parameters?pattern=2');
189189
$this->assertResponseIsSuccessful();
190190
}
191+
192+
public function testTranslationValidation(): void
193+
{
194+
$res = self::createClient()->request('GET', 'translate_validation_error', ['headers' => ['accept-language' => 'es']]);
195+
$this->assertSame([
196+
['propertyPath' => 'name', 'message' => 'Este valor no debería estar vacío.', 'code' => 'c1051bb4-d103-4f74-8988-acbcafc7fdc3'],
197+
['propertyPath' => 'surname', 'message' => 'Este valor no debería ser nulo.', 'code' => 'ad32d13f-c3d4-423b-909a-857b961eb720']
198+
199+
], $res->toArray(false)['violations']);
200+
}
191201
}

0 commit comments

Comments
 (0)