mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-23 12:44:12 -08:00
Fix nested checkboxes being updated as a group
This commit is contained in:
parent
aec781e7df
commit
9a73311a99
|
@ -24,9 +24,8 @@ class CustomFieldSetDefaultValuesForModel extends Component
|
|||
$this->fieldset_id = $this->model?->fieldset_id;
|
||||
$this->add_default_values = ($this->model?->defaultValues->count() > 0);
|
||||
|
||||
$this->fields->each(function ($field) {
|
||||
$this->selectedValues[$field->db_column] = $this->getSelectedValueForField($field);
|
||||
});
|
||||
$this->initializeSelectedValuesArray();
|
||||
$this->populatedSelectedValuesArray();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
|
@ -52,6 +51,39 @@ class CustomFieldSetDefaultValuesForModel extends Component
|
|||
return view('livewire.custom-field-set-default-values-for-model');
|
||||
}
|
||||
|
||||
/**
|
||||
* Livewire property binding plays nicer with arrays when it knows
|
||||
* which keys will be present instead of them being
|
||||
* dynamically added (this is especially true for checkboxes).
|
||||
*
|
||||
* Let's go ahead and initialize selectedValues with all the potential keys (custom field db_columns).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function initializeSelectedValuesArray(): void
|
||||
{
|
||||
CustomField::all()->each(function ($field) {
|
||||
$this->selectedValues[$field->db_column] = null;
|
||||
|
||||
if ($field->element === 'checkbox') {
|
||||
$this->selectedValues[$field->db_column] = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the selectedValues array with the
|
||||
* default values or old input for each field.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function populatedSelectedValuesArray(): void
|
||||
{
|
||||
$this->fields->each(function ($field) {
|
||||
$this->selectedValues[$field->db_column] = $this->getSelectedValueForField($field);
|
||||
});
|
||||
}
|
||||
|
||||
private function getSelectedValueForField(CustomField $field)
|
||||
{
|
||||
$defaultValue = $field->defaultValue($this->model_id);
|
||||
|
|
Loading…
Reference in a new issue