Skip to content

Commit fafbe5c

Browse files
authored
fix(validator): custom message was not translated (#7424)
fixes #7336
1 parent 563cd63 commit fafbe5c

File tree

7 files changed

+62
-6
lines changed

7 files changed

+62
-6
lines changed

features/filter/filter_validation.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Feature: Validate filters based upon filter description
1616
Scenario: Required filter should throw an error if not set
1717
When I am on "/filter_validators"
1818
Then the response status code should be 422
19-
And the JSON node "detail" should be equal to 'required: This value should not be blank.\nrequired-allow-empty: The parameter "required-allow-empty" is required.'
19+
And the JSON node "detail" should be equal to 'required: This value should not be blank.\nrequired-allow-empty: This value should not be null.'
2020

2121
Scenario: Required filter should not throw an error if set
2222
When I am on "/array_filter_validators?arrayRequired[]=foo&indexedArrayRequired[foo]=foo"

features/http_cache/headers.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ Feature: Default values of HTTP cache headers
99
Then the response status code should be 200
1010
And the header "Etag" should be equal to '"032297ac74d75a50"'
1111
And the header "Cache-Control" should be equal to "max-age=60, public, s-maxage=3600"
12-
And the header "Vary" should be equal to "Accept, Cookie"
12+
And the header "Vary" should be equal to "Accept, Cookie, Accept-Language"

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,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\ValidateParameterBeforeProvider;
1819
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter;
1920
use ApiPlatform\Tests\SetupClassResourcesTrait;
@@ -29,13 +30,13 @@ final class ValidationTest extends ApiTestCase
2930
*/
3031
public static function getResources(): array
3132
{
32-
return [WithParameter::class, ValidateParameterBeforeProvider::class];
33+
return [WithParameter::class, ValidateParameterBeforeProvider::class, TranslateValidationError::class];
3334
}
3435

3536
public function testWithGroupFilter(): void
3637
{
3738
$response = self::createClient()->request('GET', 'with_parameters_collection');
38-
$this->assertArraySubset(['violations' => [['message' => 'The parameter "hydra" is required.']]], $response->toArray(false));
39+
$this->assertArraySubset(['violations' => [['message' => 'This value should not be null.']]], $response->toArray(false));
3940
$response = self::createClient()->request('GET', 'with_parameters_collection?hydra');
4041
$this->assertResponseIsSuccessful();
4142
}
@@ -188,4 +189,13 @@ public function testValidatePattern(): void
188189
self::createClient()->request('GET', 'validate_parameters?pattern=2');
189190
$this->assertResponseIsSuccessful();
190191
}
192+
193+
public function testTranslationValidation(): void
194+
{
195+
$res = self::createClient()->request('GET', 'translate_validation_error', ['headers' => ['accept-language' => 'es']]);
196+
$this->assertSame([
197+
['propertyPath' => 'name', 'message' => 'Este valor no debería estar vacío.', 'code' => 'c1051bb4-d103-4f74-8988-acbcafc7fdc3'],
198+
['propertyPath' => 'surname', 'message' => 'Este valor no debería ser nulo.', 'code' => 'ad32d13f-c3d4-423b-909a-857b961eb720'],
199+
], $res->toArray(false)['violations']);
200+
}
191201
}

0 commit comments

Comments
 (0)