Skip to content

Commit

Permalink
In Nested rule call afterInitAttribute() in nested rules (#438)
Browse files Browse the repository at this point in the history
* In `Nested` rule call `afterInitAttribute()` in nested rules

* Apply fixes from StyleCI

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
vjik and StyleCIBot committed Dec 8, 2022
1 parent 5db30c0 commit c5e9e93
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Rule/Nested.php
Expand Up @@ -11,6 +11,7 @@
use ReflectionProperty;
use Traversable;
use Yiisoft\Strings\StringHelper;
use Yiisoft\Validator\AfterInitAttributeEventInterface;
use Yiisoft\Validator\PropagateOptionsInterface;
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
Expand Down Expand Up @@ -44,7 +45,8 @@ final class Nested implements
SkipOnErrorInterface,
WhenInterface,
SkipOnEmptyInterface,
PropagateOptionsInterface
PropagateOptionsInterface,
AfterInitAttributeEventInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
Expand Down Expand Up @@ -307,6 +309,21 @@ public function propagateOptions(): void
$this->rules = $rules;
}

public function afterInitAttribute(object $object): void
{
if ($this->rules === null) {
return;
}

foreach ($this->rules as $rules) {
foreach ((is_iterable($rules) ? $rules : [$rules]) as $rule) {
if ($rule instanceof AfterInitAttributeEventInterface) {
$rule->afterInitAttribute($object);
}
}
}
}

#[ArrayShape([
'requirePropertyPath' => 'bool',
'noRulesWithNoObjectMessage' => 'array',
Expand Down
34 changes: 34 additions & 0 deletions tests/Support/Data/NestedWithCallbackAttribute.php
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Validator\Tests\Support\Data;

use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule\Callback;
use Yiisoft\Validator\Rule\Nested;

#[Nested([
'a' => new Callback(method: 'validateA'),
])]
final class NestedWithCallbackAttribute
{
private int $a = 7;

#[Nested([
'x' => new Callback(method: 'validateB'),
])]
private array $b = [
'x' => 5,
];

private function validateA(): Result
{
return (new Result())->addError('Invalid A.');
}

private function validateB(): Result
{
return (new Result())->addError('Invalid B.');
}
}
14 changes: 14 additions & 0 deletions tests/TestEnvironments/Php81/Rule/NestedTest.php
Expand Up @@ -9,6 +9,7 @@
use Yiisoft\Validator\Rule\Nested;
use Yiisoft\Validator\Rule\Number;
use Yiisoft\Validator\Tests\Support\Data\NestedClassAttribute;
use Yiisoft\Validator\Tests\Support\Data\NestedWithCallbackAttribute;
use Yiisoft\Validator\Tests\Support\Data\ObjectWithDifferentPropertyVisibility;
use Yiisoft\Validator\Validator;

Expand Down Expand Up @@ -79,4 +80,17 @@ public function testClassAttribute(): void
$result->getErrorMessagesIndexedByAttribute()
);
}

public function testWithCallbackAttribute(): void
{
$result = (new Validator())->validate(new NestedWithCallbackAttribute());

$this->assertSame(
[
'a' => ['Invalid A.'],
'b' => ['Invalid B.'],
],
$result->getErrorMessagesIndexedByAttribute()
);
}
}

0 comments on commit c5e9e93

Please sign in to comment.