Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.3.1 under development

- Bug #750: Fix `WhenMissing` not working in nested rules (@Enjoyzz)
- Enh #770: Allow use callable rules into `Nested` (@Enjoyzz)
- Bug #751: Fix incorrect `Nested` rule processing within `Each` (@Enjoyzz)

Expand Down
5 changes: 4 additions & 1 deletion src/Rule/NestedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@
} else {
$valuePathList = StringHelper::parsePath($valuePath);
$property = end($valuePathList);
$itemResult = $context->validate([$property => $validatedValue], [$property => $rules]);
$itemResult = $context->validate(
ArrayHelper::keyExists($data, $valuePathList) ? [$property => $validatedValue] : [],
[$property => $rules]
);

Check warning on line 99 in src/Rule/NestedHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Rule/NestedHandler.php#L96-L99

Added lines #L96 - L99 were not covered by tests
}

if ($itemResult->isValid()) {
Expand Down
115 changes: 114 additions & 1 deletion tests/Rule/NestedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use stdClass;
use Yiisoft\Validator\DataSet\ObjectDataSet;
use Yiisoft\Validator\DataSetInterface;
use Yiisoft\Validator\EmptyCondition\WhenMissing;
use Yiisoft\Validator\Error;
use Yiisoft\Validator\Helper\RulesDumper;
use Yiisoft\Validator\PostValidationHookInterface;
Expand All @@ -29,6 +30,7 @@
use Yiisoft\Validator\Rule\Regex;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\Rule\StringValue;
use Yiisoft\Validator\Rule\Type\BooleanType;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\RulesProviderInterface;
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
Expand Down Expand Up @@ -1259,7 +1261,7 @@ public function hasProperty(string $property): bool
ReflectionProperty::IS_PUBLIC,
),
new Nested(['value' => new Required()]),
['value' => ['Value cannot be blank.']],
['value' => ['Value not passed.']],
],
'nested context' => [
[
Expand Down Expand Up @@ -1576,4 +1578,115 @@ public function testExampleFromIssue751()
$this->assertFalse($result->isValid());
$this->assertSame(['array_of_a.0.id' => ['Id cannot be blank.']], $result->getErrorMessagesIndexedByPath());
}

/**
* @see https://github.com/yiisoft/validator/issues/750
*/
public function testNestedRulesWithWhenMissing(): void
{
$rules = [
'regulations' => new Nested(
rules: [
'is_one_time' => new BooleanType(
skipOnEmpty: new WhenMissing()
),
],
skipOnEmpty: new WhenMissing()
),
];

$data = [
'service' => 'service',
'amount' => 200,
];

$result = (new Validator())->validate($data, $rules);
$this->assertTrue($result->isValid());

$data = [
'service' => 'service',
'amount' => 200,
'regulations' => [],
];

$result = (new Validator())->validate($data, $rules);
$this->assertTrue($result->isValid());

$data = [
'service' => 'service',
'amount' => 200,
'regulations' => [
'is_one_time' => null,
],
];

$result = (new Validator())->validate($data, $rules);
$this->assertFalse($result->isValid());

$data = [
'service' => 'service',
'amount' => 200,
'regulations' => [
'is_one_time' => true,
],
];

$result = (new Validator())->validate($data, $rules);
$this->assertTrue($result->isValid());
}

public function testNestedRulesWithWhenMissingAndNullableField(): void
{
$rules = [
'regulations' => new Nested(
rules: [
'is_one_time' => new Callback(
callback: static function (mixed $value): Result {
$result = new Result();
if (!in_array(get_debug_type($value), ['bool', 'null'], true)) {
$result->addError('Value must be a boolean or null.');
}
return $result;
},
skipOnEmpty: new WhenMissing()
),
],
skipOnEmpty: new WhenMissing()
),
];

$data = [
'service' => 'service',
'amount' => 200,
'regulations' => [
'is_one_time' => null,
],
];

$result = (new Validator())->validate($data, $rules);
$this->assertTrue($result->isValid());


$data = [
'service' => 'service',
'amount' => 200,
'regulations' => [
'is_one_time' => true,
],
];

$result = (new Validator())->validate($data, $rules);
$this->assertTrue($result->isValid());

$data = [
'service' => 'service',
'amount' => 200,
'regulations' => [
'is_one_time' => '',
],
];

$result = (new Validator())->validate($data, $rules);
$this->assertFalse($result->isValid());
}
}
Loading