Skip to content

Commit b10c4af

Browse files
authored
Take PropertyTranslatorProviderInterface into account during context validation (#776)
1 parent 0426e76 commit b10c4af

7 files changed

Lines changed: 72 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 2.5.1 under development
44

5-
- no changes in this release.
5+
- Bug #776: Take `PropertyTranslatorProviderInterface` into account during context validation (@vjik)
66

77
## 2.5.0 July 19, 2025
88

src/Rule/Nested.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use Yiisoft\Validator\RulesProviderInterface;
2626
use Yiisoft\Validator\SkipOnEmptyInterface;
2727
use Yiisoft\Validator\SkipOnErrorInterface;
28-
use Yiisoft\Validator\Tests\Rule\NestedTest;
2928
use Yiisoft\Validator\WhenInterface;
3029

3130
use function array_pop;

src/ValidationContext.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,20 @@ public function validate(mixed $data, callable|iterable|object|string|null $rule
156156
$currentProperty = $this->property;
157157
$isCurrentDataSetMissing = $this->isDataSetMissing;
158158
$currentParameters = $this->parameters;
159+
$currentDefaultPropertyTranslator = $this->defaultPropertyTranslator;
159160

160161
// The lack of a property means that in the context of further validation there is no data set at all.
161162
$this->isDataSetMissing = $this->isPropertyMissing();
163+
if ($data instanceof PropertyTranslatorProviderInterface) {
164+
$this->defaultPropertyTranslator = $data->getPropertyTranslator() ?? $currentDefaultPropertyTranslator;
165+
}
162166
$result = $this->validator->validate($data, $rules, $this);
163167

164168
$this->dataSet = $currentDataSet;
165169
$this->property = $currentProperty;
166170
$this->isDataSetMissing = $isCurrentDataSetMissing;
167171
$this->parameters = $currentParameters;
172+
$this->defaultPropertyTranslator = $currentDefaultPropertyTranslator;
168173

169174
return $result;
170175
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator\Tests\Rule\Nested\NestedPropertyTranslator;
6+
7+
use Yiisoft\Validator\Rule\Nested;
8+
9+
final class MainForm
10+
{
11+
public function __construct(
12+
#[Nested]
13+
public SubForm $sub,
14+
) {
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator\Tests\Rule\Nested\NestedPropertyTranslator;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Validator\Validator;
9+
10+
use function PHPUnit\Framework\assertSame;
11+
12+
final class NestedPropertyTranslatorTest extends TestCase
13+
{
14+
public function testBase(): void
15+
{
16+
$form = new MainForm(new SubForm());
17+
$validator = new Validator();
18+
19+
$result = $validator->validate($form);
20+
21+
assertSame(
22+
['Телефон must contain at least 5 characters.'],
23+
$result->getErrorMessages()
24+
);
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator\Tests\Rule\Nested\NestedPropertyTranslator;
6+
7+
use Yiisoft\Validator\PropertyTranslator\ArrayPropertyTranslator;
8+
use Yiisoft\Validator\PropertyTranslatorInterface;
9+
use Yiisoft\Validator\PropertyTranslatorProviderInterface;
10+
use Yiisoft\Validator\Rule\Length;
11+
12+
final class SubForm implements PropertyTranslatorProviderInterface
13+
{
14+
#[Length(min: 5)]
15+
public string $phone = '123';
16+
17+
public function getPropertyTranslator(): ?PropertyTranslatorInterface
18+
{
19+
return new ArrayPropertyTranslator([
20+
'phone' => 'Телефон',
21+
]);
22+
}
23+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Yiisoft\Validator\Tests\Rule;
5+
namespace Yiisoft\Validator\Tests\Rule\Nested;
66

77
use ArrayObject;
88
use InvalidArgumentException;

0 commit comments

Comments
 (0)