Skip to content

Commit 49db43b

Browse files
committed
fix: use cast data object when duplicating option settings
getRawOriginal('settings') returns a raw JSON string, but DataEloquentCast expects a Data object or array. Pass the hydrated settings property instead.
1 parent 178ead6 commit 49db43b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Livewire/ManageCustomField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function duplicateAction(): Action
9292
$clone->options()->create([
9393
'name' => $option->getRawOriginal('name'),
9494
'sort_order' => $option->sort_order,
95-
'settings' => $option->getRawOriginal('settings'),
95+
'settings' => $option->settings,
9696
]);
9797
}
9898

tests/Feature/Admin/Pages/CustomFieldsFieldManagementTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,38 @@
386386
->toBe(['Alpha', 'Bravo', 'Charlie']);
387387
});
388388

389+
it('can duplicate a select field and preserve option settings', function (): void {
390+
$field = CustomField::factory()
391+
->ofType('select')
392+
->withOptions(['Red', 'Blue'])
393+
->create([
394+
'custom_field_section_id' => $this->section->getKey(),
395+
'entity_type' => $this->userEntityType,
396+
'name' => 'Color Picker',
397+
'code' => 'color_picker',
398+
]);
399+
400+
$field->options->first()->update(['settings' => new \Relaticle\CustomFields\Data\CustomFieldOptionSettingsData(color: '#ff0000')]);
401+
$field->options->last()->update(['settings' => new \Relaticle\CustomFields\Data\CustomFieldOptionSettingsData(color: '#0000ff')]);
402+
403+
livewire(ManageCustomField::class, [
404+
'field' => $field->fresh(),
405+
])->callAction('duplicate');
406+
407+
$clone = CustomField::query()
408+
->withDeactivated()
409+
->where('code', 'color-picker-copy')
410+
->first();
411+
412+
expect($clone)->not->toBeNull();
413+
expect($clone->options)->toHaveCount(2);
414+
415+
$clonedOptions = $clone->options->sortBy('sort_order')->values();
416+
417+
expect($clonedOptions[0]->settings->color)->toBe('#ff0000');
418+
expect($clonedOptions[1]->settings->color)->toBe('#0000ff');
419+
});
420+
389421
it('generates unique code when duplicating a field with existing copy', function (): void {
390422
$field = CustomField::factory()
391423
->ofType('text')

0 commit comments

Comments
 (0)