Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions src/Fieldtypes/HasSelectOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,18 @@ public function preProcess($value)
return [];
}

$keys = collect($this->getOptions())->pluck('value');

// NOTE: Null-coalescing into `[null]` as that matches old behaviour.
$values = collect($value ?? [null])->map(function ($value) {
return $this->config('cast_booleans') ? $this->castFromBoolean($value) : $value;
$values = collect($value ?? [null])->map(function ($value) use ($keys) {
$value = $this->config('cast_booleans') ? $this->castFromBoolean($value) : $value;

// Front-end form submissions store numeric keys as strings, e.g. '1' for an option key of 1.
if (is_string($value) && ! $keys->containsStrict($value)) {
$value = $keys->first(fn ($key) => is_int($key) && (string) $key === $value) ?? $value;
}

return $value;
});

return $multiple ? $values->all() : $values->first();
Expand Down
23 changes: 23 additions & 0 deletions tests/Fieldtypes/HasSelectOptionsTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ public static function preProcessIndexProvider()
'two',
['two'],
],
'numeric keys with string value' => [
[
['key' => 926450000, 'value' => 'One'],
['key' => 926450001, 'value' => 'Two'],
],
'926450001',
['Two'],
],
'numeric keys with differently formatted string value' => [
[
['key' => 1, 'value' => 'One'],
],
'01',
['01'],
],
'numeric and string keys with string value' => [
[
['key' => 1, 'value' => 'One'],
['key' => '1', 'value' => 'String One'],
],
'1',
['String One'],
],
];
}
}
Loading