Skip to content

Commit c73cf7b

Browse files
committed
fix: return nothing when optional param absent
1 parent f58fc91 commit c73cf7b

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/Form.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,17 @@ public function validate(array $dataSource, array $validationSet)
287287
if (!$this->test($userRules, $value, $itemToValidate)) {
288288
$output = false;
289289
} else if ($output !== false && !$endsWithWildcard) {
290+
if (
291+
(is_array($userRules) && in_array('optional', $userRules))
292+
|| (is_string($userRules) && strpos($userRules, 'optional') !== false)
293+
) {
294+
if (Anchor::deepGetDot($dataSource, $itemToValidate) !== null) {
295+
$output = Anchor::deepSetDot($output, $itemToValidate, $value);
296+
}
297+
298+
continue;
299+
}
300+
290301
$output = Anchor::deepSetDot($output, $itemToValidate, $value);
291302
}
292303
}

tests/rules.test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
'item2' => 'not custom',
3737
];
3838

39-
expect(validator()->validate($data, ['item1' => 'anotherCustomRule']))->toBe($data);
39+
expect(validator()->validate($data, ['item1' => 'anotherCustomRule']))->toBe([
40+
'item1' => 'custom',
41+
]);
4042
expect(validator()->validate($data, ['item2' => 'anotherCustomRule']))->toBe(false);
4143
expect(validator()->errors())->toHaveKey('item2');
4244
});

0 commit comments

Comments
 (0)