Skip to content

Commit

Permalink
prefer "not empty" over "not null" for better UX
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Oct 14, 2023
1 parent 0b343f3 commit 0bb6ff0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ public function normalize($value)
$value = $this->normalizeUsingTypecast($value);

if ($value === null) {
if (!$this->nullable || $this->required) {
if ($this->required) {
throw new Exception('Must not be empty');
} elseif (!$this->nullable) {
throw new Exception('Must not be null');
}

Expand Down
15 changes: 13 additions & 2 deletions tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,18 @@ public function testRequiredNullException(): void
$m = $m->createEntity();

$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Must not be null');
$this->expectExceptionMessage('Must not be empty');
$m->set('foo', null);
}

public function testNotNullableAndRequiredNullException(): void
{
$m = new Model();
$m->addField('foo', ['nullable' => false, 'required' => true]);
$m = $m->createEntity();

$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Must not be empty');
$m->set('foo', null);
}

Expand Down Expand Up @@ -859,7 +870,7 @@ public function testSetNull(): void
self::assertNull($m->get('c'));

$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Must not be null');
$this->expectExceptionMessage('Must not be empty');
$m->set('c', null);
}

Expand Down

0 comments on commit 0bb6ff0

Please sign in to comment.