Skip to content

Commit fac81af

Browse files
committed
Check if record exists (for CreateRecord pages)
1 parent b4d41e7 commit fac81af

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/Concerns/HasFieldsMapper.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function refresh(): void
5555

5656
protected function mutateFormDataBeforeFill(array $data): array
5757
{
58-
if ($this->record->fields->isEmpty()) {
58+
if (! isset($this->record) || $this->record->fields->isEmpty()) {
5959
return $data;
6060
}
6161

@@ -72,6 +72,10 @@ protected function mutateFormDataBeforeFill(array $data): array
7272

7373
protected function mutateFormDataBeforeSave(array $data): array
7474
{
75+
if (! isset($this->record)) {
76+
return $data;
77+
}
78+
7579
$data = $this->mutateFormData($data, function ($field, $fieldConfig, $fieldInstance, $data) {
7680
if (! empty($fieldConfig['methods']['mutateBeforeSaveCallback'])) {
7781
return $fieldInstance->mutateBeforeSaveCallback($this->record, $field, $data);
@@ -99,14 +103,14 @@ protected function mutateFormData(array $data, callable $mutationStrategy): arra
99103

100104
private function resolveFormFields(): array
101105
{
102-
if ($this->record->fields->isEmpty()) {
106+
if (! isset($this->record) || $this->record->fields->isEmpty()) {
103107
return [];
104108
}
105109

106110
$customFields = $this->resolveCustomFields();
107111

108112
return $this->record->fields
109-
->map(fn ($field) => $this->resolveFieldInput($field, $customFields))
113+
->map(fn($field) => $this->resolveFieldInput($field, $customFields))
110114
->filter()
111115
->values()
112116
->all();
@@ -115,7 +119,7 @@ private function resolveFormFields(): array
115119
private function resolveCustomFields(): Collection
116120
{
117121
return collect(Fields::getFields())
118-
->map(fn ($fieldClass) => new $fieldClass);
122+
->map(fn($fieldClass) => new $fieldClass);
119123
}
120124

121125
private function resolveFieldInput(Model $field, Collection $customFields): ?object
@@ -134,4 +138,4 @@ private function resolveFieldInput(Model $field, Collection $customFields): ?obj
134138

135139
return null;
136140
}
137-
}
141+
}

0 commit comments

Comments
 (0)