fix: duplicate action crashes on option settings cast#97
Conversation
getRawOriginal('settings') returns a raw JSON string, but
DataEloquentCast expects a Data object or array. Pass the hydrated
settings property instead.
There was a problem hiding this comment.
Pull request overview
Fixes a crash when duplicating custom fields with options by ensuring option settings are duplicated using the hydrated Spatie Data object rather than the raw JSON string.
Changes:
- Update duplicate action to copy
$option->settings(casted Data) instead ofgetRawOriginal('settings')(raw JSON). - Add a regression test to verify select-field duplication preserves option color settings.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Livewire/ManageCustomField.php |
Adjusts option duplication to use the casted settings attribute during clone creation. |
tests/Feature/Admin/Pages/CustomFieldsFieldManagementTest.php |
Adds a regression test ensuring option settings (colors) are preserved after duplication. |
| @@ -121,7 +121,7 @@ public function duplicateAction(): Action | |||
| $clone->options()->create([ | |||
| 'name' => $option->getRawOriginal('name'), | |||
There was a problem hiding this comment.
In duplicateAction(), using $option->getRawOriginal('name') will pass the encrypted DB value when the parent field has encryption enabled; the CustomFieldOption::saving hook will then encrypt again, resulting in double-encrypted option names on the cloned field. Use the decrypted accessor value (e.g., $option->name) or adjust the saving hook to avoid re-encrypting already-encrypted values so duplicated encrypted fields preserve readable option names.
| 'name' => $option->getRawOriginal('name'), | |
| 'name' => $option->name, |
| $field->options->first()->update(['settings' => new \Relaticle\CustomFields\Data\CustomFieldOptionSettingsData(color: '#ff0000')]); | ||
| $field->options->last()->update(['settings' => new \Relaticle\CustomFields\Data\CustomFieldOptionSettingsData(color: '#0000ff')]); |
There was a problem hiding this comment.
This test instantiates \Relaticle\CustomFields\Data\CustomFieldOptionSettingsData via a fully-qualified name. The rest of this file imports dependencies at the top (see use statements around lines 5–13), so consider adding an import for CustomFieldOptionSettingsData and using the short class name here for consistency/readability.
Summary
getRawOriginal('settings')returns a raw JSON string, butDataEloquentCast::set()expects aDataobject or array — causing aCannotCastDataexception when duplicating a field with options$option->settingswhich passes the hydratedCustomFieldOptionSettingsDataobjectBackport of #96 for 2.x.
Test plan
can duplicate a select field and preserve option settings