From 54ac2b2129c2d2f17b1d21567f9b77dbba4846ea Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Fri, 28 May 2021 14:45:51 +0200 Subject: [PATCH] fix empty return --- src/JSON.php | 4 ++-- tests/JSONTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/JSON.php b/src/JSON.php index 0769173..1a4c60b 100644 --- a/src/JSON.php +++ b/src/JSON.php @@ -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; } @@ -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]; } /** diff --git a/tests/JSONTest.php b/tests/JSONTest.php index 0b9e2e8..c4d3a31 100644 --- a/tests/JSONTest.php +++ b/tests/JSONTest.php @@ -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() {