Selection when $state is null

This seems dirty, but works. Is this the only way automatically select an option when the state is null? If i do not do this string workaround, nothing gets selected when the state is null.
->options([
'' => 'None',
'1' => '1x',
'1.5' => '1.5x',
'2' => '2x',
'2.5' => '2.5x',
'3' => '3x',
])
->formatStateUsing(fn ($state) => $state ?? '')
->dehydrateStateUsing(fn ($state) => $state === '' ? null : $state)
->options([
'' => 'None',
'1' => '1x',
'1.5' => '1.5x',
'2' => '2x',
'2.5' => '2.5x',
'3' => '3x',
])
->formatStateUsing(fn ($state) => $state ?? '')
->dehydrateStateUsing(fn ($state) => $state === '' ? null : $state)
I would prefer to just do
->options([
null => 'None',
'1' => '1x',
'1.5' => '1.5x',
'2' => '2x',
'2.5' => '2.5x',
'3' => '3x',
])
->options([
null => 'None',
'1' => '1x',
'1.5' => '1.5x',
'2' => '2x',
'2.5' => '2.5x',
'3' => '3x',
])
1 Reply
awcodes
awcodes10h ago
The problem is that select options can’t actually be null. In the html element null would be a string ‘null’ unless something internally converts null to an empty string.

Did you find this page helpful?