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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## Unreleased

### Changed

- Validation treats `0`, `"0"`, and `false` as present values. Required fields now pass these values on to their validation rules, and optional fields validate them when supplied. For example, `optional|email` now rejects `false` as an invalid email address. Missing fields, `null`, empty strings, and empty arrays still count as absent.
28 changes: 28 additions & 0 deletions tests/validation.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,31 @@
expect($validatedData)->toBe($itemsToValidate);
expect(validator()->errors())->not()->toHaveKey('test6');
});

test('falsy boolean values are treated as present', function ($value) {
$form = new \Leaf\Form();
$data = ['flag' => $value];

expect($form->validate($data, ['flag' => 'boolean']))->toBe($data);
expect($form->validate($data, ['flag' => 'optional|boolean']))->toBe($data);
expect($form->validate($data, ['flag' => 'optional|email']))->toBe(false);
expect($form->errors()['flag'])->toBe('Flag must be a valid email address');
})->with([
'integer zero' => [0],
'string zero' => ['0'],
'false' => [false],
]);

test('absent values still fail required validation and bypass optional rules', function ($value) {
$form = new \Leaf\Form();
$data = ['flag' => $value];

expect($form->validate($data, ['flag' => 'boolean']))->toBe(false);
expect($form->errors()['flag'])->toBe('Flag is required');
expect($form->validate($data, ['flag' => 'optional|email']))->toBe($value === null ? [] : $data);
expect($form->errors())->toBe([]);
})->with([
'null' => [null],
'empty string' => [''],
'empty array' => [[]],
]);
Loading