diff --git a/CHANGELOG.md b/CHANGELOG.md index 07cae8b2..c9f09e7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 2.5.1 under development -- no changes in this release. +- Bug #776: Take `PropertyTranslatorProviderInterface` into account during context validation (@vjik) ## 2.5.0 July 19, 2025 diff --git a/src/Rule/Nested.php b/src/Rule/Nested.php index fddc795c..fe1e5db2 100644 --- a/src/Rule/Nested.php +++ b/src/Rule/Nested.php @@ -25,7 +25,6 @@ use Yiisoft\Validator\RulesProviderInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; -use Yiisoft\Validator\Tests\Rule\NestedTest; use Yiisoft\Validator\WhenInterface; use function array_pop; diff --git a/src/ValidationContext.php b/src/ValidationContext.php index 54654642..c29851cd 100644 --- a/src/ValidationContext.php +++ b/src/ValidationContext.php @@ -156,15 +156,20 @@ public function validate(mixed $data, callable|iterable|object|string|null $rule $currentProperty = $this->property; $isCurrentDataSetMissing = $this->isDataSetMissing; $currentParameters = $this->parameters; + $currentDefaultPropertyTranslator = $this->defaultPropertyTranslator; // The lack of a property means that in the context of further validation there is no data set at all. $this->isDataSetMissing = $this->isPropertyMissing(); + if ($data instanceof PropertyTranslatorProviderInterface) { + $this->defaultPropertyTranslator = $data->getPropertyTranslator() ?? $currentDefaultPropertyTranslator; + } $result = $this->validator->validate($data, $rules, $this); $this->dataSet = $currentDataSet; $this->property = $currentProperty; $this->isDataSetMissing = $isCurrentDataSetMissing; $this->parameters = $currentParameters; + $this->defaultPropertyTranslator = $currentDefaultPropertyTranslator; return $result; } diff --git a/tests/Rule/Nested/NestedPropertyTranslator/MainForm.php b/tests/Rule/Nested/NestedPropertyTranslator/MainForm.php new file mode 100644 index 00000000..9b63d5f3 --- /dev/null +++ b/tests/Rule/Nested/NestedPropertyTranslator/MainForm.php @@ -0,0 +1,16 @@ +validate($form); + + assertSame( + ['Телефон must contain at least 5 characters.'], + $result->getErrorMessages() + ); + } +} diff --git a/tests/Rule/Nested/NestedPropertyTranslator/SubForm.php b/tests/Rule/Nested/NestedPropertyTranslator/SubForm.php new file mode 100644 index 00000000..7750b927 --- /dev/null +++ b/tests/Rule/Nested/NestedPropertyTranslator/SubForm.php @@ -0,0 +1,23 @@ + 'Телефон', + ]); + } +} diff --git a/tests/Rule/NestedTest.php b/tests/Rule/Nested/NestedTest.php similarity index 99% rename from tests/Rule/NestedTest.php rename to tests/Rule/Nested/NestedTest.php index 24102496..3c4f1130 100644 --- a/tests/Rule/NestedTest.php +++ b/tests/Rule/Nested/NestedTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Validator\Tests\Rule; +namespace Yiisoft\Validator\Tests\Rule\Nested; use ArrayObject; use InvalidArgumentException;