From 0abb5637e08551fa06611741212ffa51f258386f Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 22 Aug 2024 12:54:54 -0700 Subject: [PATCH] WIP: handle old input --- .../CustomFieldSetDefaultValuesForModel.php | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/app/Livewire/CustomFieldSetDefaultValuesForModel.php b/app/Livewire/CustomFieldSetDefaultValuesForModel.php index 8f7de769fa..7889c2a691 100644 --- a/app/Livewire/CustomFieldSetDefaultValuesForModel.php +++ b/app/Livewire/CustomFieldSetDefaultValuesForModel.php @@ -2,6 +2,7 @@ namespace App\Livewire; +use App\Models\CustomField; use Livewire\Attributes\Computed; use Livewire\Component; @@ -24,12 +25,11 @@ class CustomFieldSetDefaultValuesForModel extends Component $this->add_default_values = ($this->model?->defaultValues->count() > 0); $this->fields->each(function ($field) { - if ($field->element === 'checkbox') { - $this->selectedValues[$field->db_column] = explode(', ', $field->defaultValue($this->model_id)); - } else { - $this->selectedValues[$field->db_column] = $field->defaultValue($this->model_id); - } + $this->setSelectedValueForField($field); }); + + dump(old('default_values')); + dump($this->selectedValues); } #[Computed] @@ -54,4 +54,23 @@ class CustomFieldSetDefaultValuesForModel extends Component { return view('livewire.custom-field-set-default-values-for-model'); } + + private function setSelectedValueForField(CustomField $field): void + { + $defaultValue = $field->defaultValue($this->model_id); + + if (old('default_values.' . $field->id)) { + // @todo: need to handle old input being null on purpose... + $defaultValue = old('default_values.' . $field->id); + } + + // on first load the default value for checkboxes will be + // a comma-separated string but if we're loading the page + // with old input then it was already parsed into an array. + if ($field->element === 'checkbox' && is_string($defaultValue)) { + $this->selectedValues[$field->db_column] = explode(', ', $defaultValue); + } else { + $this->selectedValues[$field->db_column] = $defaultValue; + } + } }