Skip to content

Commit

Permalink
fix empty return
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed May 28, 2021
1 parent 78910e9 commit 54ac2b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public function fillAtOnce(callable $fillAtOnceCallback = null): self
? $fillAtOnceCallback($request, $requestValues, $model, $attribute, $requestAttribute)
: $requestValues;

if ($this->nullable && $this->isNullValue($value)) {
if (!$this->nullable && $this->isNullValue($value)) {
return;
}

Expand Down Expand Up @@ -323,7 +323,7 @@ protected function getRequestValues(NovaRequest $request, Model $model): array
data_set($carry, $path, $item);

return $carry;
}, [])[$this->attribute];
}, [$this->attribute => []])[$this->attribute];
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/JSONTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ public function it_respects_the_fill_all_values_at_once_callback_and_individual_
$this->assertEquals(['nested' => ['street' => 'some-val Foo', 'city' => 'other-val']], $user->address);
}

/** @test */
public function it_does_not_store_values_for_fill_once_if_fields_are_not_nullable_and_nothing_is_returned_from_callback()
{
$user = new User(['address' => ['street' => '', 'city' => '']]);
$json = JSON::make('Address', 'address', [
Text::make('Street'),
Text::make('City'),
])->nullable(false)->fillAtOnce(fn ($request, $requestValues) => null);

$request = new NovaRequest(['address->street' => 'some-val', 'address->city' => 'other-val', 'nonjson' => 'foo']);

collect($json->data)->last()->fillInto($request, $user, 'address');
$this->assertEquals(['street' => '', 'city' => ''], $user->address);
}

/** @test */
public function it_allows_storing_nullable_values()
{
Expand Down

0 comments on commit 54ac2b2

Please sign in to comment.