Skip to content

fix: duplicate action crashes on option settings cast#96

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

fix: duplicate action crashes on option settings cast#96
ManukMinasyan merged 1 commit into3.xfrom
fix/duplicate-option-settings-cast

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

Test plan

  • Added regression test: can duplicate a select field and preserve option settings
  • All existing duplicate tests pass
  • Full field management test suite passes (40 tests)

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:34
@ManukMinasyan ManukMinasyan merged commit 3157dd5 into 3.x Mar 6, 2026
1 check failed
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 that have option settings stored via a Spatie Laravel Data Eloquent cast, ensuring option settings are correctly preserved during duplication.

Changes:

  • Update the field duplication action to pass the hydrated option settings value instead of the raw JSON string.
  • Add a regression test covering duplication of select fields while preserving per-option settings (e.g., color).

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 cloning during duplication to use the cast/hydrated settings value.
tests/Feature/Admin/Pages/CustomFieldsFieldManagementTest.php Adds a regression test asserting option settings are preserved when duplicating a select field.

Comment on lines 92 to 96
$clone->options()->create([
'name' => $option->getRawOriginal('name'),
'sort_order' => $option->sort_order,
'settings' => $option->getRawOriginal('settings'),
'settings' => $option->settings,
]);
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 the duplication loop, using $option->getRawOriginal('name') can break encrypted fields: CustomFieldOption::saving() will encrypt the provided name when the parent field is encrypted, so passing an already-encrypted raw DB value will result in double encryption and unreadable option labels. Prefer duplicating with the decrypted accessor value (e.g., $option->name) and, if concerned about N+1 queries, eager-load the relation needed by the accessor before the loop or handle decryption based on $this->field->settings->encrypted.

Copilot uses AI. Check for mistakes.
Comment on lines +400 to +401
$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 uses fully-qualified \Relaticle\CustomFields\Data\CustomFieldOptionSettingsData inline. For consistency with the rest of the file (which imports its dependencies at the top) and to keep the test more readable, consider adding a use import and referencing CustomFieldOptionSettingsData directly.

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