Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions src/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,19 @@ public static function mutateBeforeSaveCallback(Model $record, Field $field, arr
* Normalize the select value to an array or a single value. This is needed because the select field can be
* changed from single to multiple or vice versa.
*/
private static function normalizeSelectValue($value, Field $field): mixed
private static function normalizeSelectValue($value, Field $field): array
{
$isMultiple = $field->config['multiple'] ?? false;

// Handle JSON string values
if (is_string($value) && json_validate($value)) {
$value = json_decode($value, true);
$value = (array) json_decode($value, true);
}

// Handle null/empty values consistently
if ($value === null || $value === '') {
return $isMultiple ? [] : null;
}

// Convert to array if multiple is expected but value is not an array
if ($isMultiple && ! is_array($value)) {
return [$value];
}

// Convert array to single value if multiple is not expected
if (! $isMultiple && is_array($value)) {
return empty($value) ? null : reset($value);
return [];
}

return $value;
return is_array($value) ? $value : [$value];
}

public function getForm(): array
Expand Down