Skip to content

Commit

Permalink
fix: return nothing when optional param absent
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Nov 8, 2024
1 parent f58fc91 commit c73cf7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,17 @@ public function validate(array $dataSource, array $validationSet)
if (!$this->test($userRules, $value, $itemToValidate)) {
$output = false;
} else if ($output !== false && !$endsWithWildcard) {
if (
(is_array($userRules) && in_array('optional', $userRules))
|| (is_string($userRules) && strpos($userRules, 'optional') !== false)
) {
if (Anchor::deepGetDot($dataSource, $itemToValidate) !== null) {
$output = Anchor::deepSetDot($output, $itemToValidate, $value);
}

continue;
}

$output = Anchor::deepSetDot($output, $itemToValidate, $value);
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/rules.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
'item2' => 'not custom',
];

expect(validator()->validate($data, ['item1' => 'anotherCustomRule']))->toBe($data);
expect(validator()->validate($data, ['item1' => 'anotherCustomRule']))->toBe([
'item1' => 'custom',
]);
expect(validator()->validate($data, ['item2' => 'anotherCustomRule']))->toBe(false);
expect(validator()->errors())->toHaveKey('item2');
});
Expand Down

0 comments on commit c73cf7b

Please sign in to comment.