mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-11 05:47:28 -08:00
WIP: handle old input
This commit is contained in:
parent
a2246b9645
commit
0abb5637e0
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Livewire;
|
namespace App\Livewire;
|
||||||
|
|
||||||
|
use App\Models\CustomField;
|
||||||
use Livewire\Attributes\Computed;
|
use Livewire\Attributes\Computed;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
|
||||||
|
@ -24,12 +25,11 @@ class CustomFieldSetDefaultValuesForModel extends Component
|
||||||
$this->add_default_values = ($this->model?->defaultValues->count() > 0);
|
$this->add_default_values = ($this->model?->defaultValues->count() > 0);
|
||||||
|
|
||||||
$this->fields->each(function ($field) {
|
$this->fields->each(function ($field) {
|
||||||
if ($field->element === 'checkbox') {
|
$this->setSelectedValueForField($field);
|
||||||
$this->selectedValues[$field->db_column] = explode(', ', $field->defaultValue($this->model_id));
|
|
||||||
} else {
|
|
||||||
$this->selectedValues[$field->db_column] = $field->defaultValue($this->model_id);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
dump(old('default_values'));
|
||||||
|
dump($this->selectedValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Computed]
|
#[Computed]
|
||||||
|
@ -54,4 +54,23 @@ class CustomFieldSetDefaultValuesForModel extends Component
|
||||||
{
|
{
|
||||||
return view('livewire.custom-field-set-default-values-for-model');
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue