Skip to content

fix: duplicate action crashes on option settings cast#97

Merged
ManukMinasyan merged 1 commit into2.xfrom
fix/duplicate-option-settings-cast-2x
Mar 6, 2026
Merged

fix: duplicate action crashes on option settings cast#97
ManukMinasyan merged 1 commit into2.xfrom
fix/duplicate-option-settings-cast-2x

Conversation

@ManukMinasyan
Copy link
Collaborator

Summary

  • getRawOriginal('settings') returns a raw JSON string, but DataEloquentCast::set() expects a Data object or array — causing a CannotCastData exception when duplicating a field with options
  • Changed to $option->settings which passes the hydrated CustomFieldOptionSettingsData object

Backport of #96 for 2.x.

Test plan

  • Added regression test: can duplicate a select field and preserve option settings
  • All existing duplicate tests pass

getRawOriginal('settings') returns a raw JSON string, but
DataEloquentCast expects a Data object or array. Pass the hydrated
settings property instead.
Copilot AI review requested due to automatic review settings March 6, 2026 15:36
@ManukMinasyan ManukMinasyan merged commit 920ca8b into 2.x Mar 6, 2026
1 check passed
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 of getRawOriginal('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'),
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
'name' => $option->getRawOriginal('name'),
'name' => $option->name,

Copilot uses AI. Check for mistakes.
Comment on lines +402 to +403
$field->options->first()->update(['settings' => new \Relaticle\CustomFields\Data\CustomFieldOptionSettingsData(color: '#ff0000')]);
$field->options->last()->update(['settings' => new \Relaticle\CustomFields\Data\CustomFieldOptionSettingsData(color: '#0000ff')]);
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants