|
11 | 11 |
|
12 | 12 | namespace Symfony\UX\LiveComponent;
|
13 | 13 |
|
| 14 | +use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView; |
14 | 15 | use Symfony\Component\Form\ClearableErrorsInterface;
|
15 | 16 | use Symfony\Component\Form\FormInterface;
|
16 | 17 | use Symfony\Component\Form\FormView;
|
@@ -255,25 +256,39 @@ private function extractFormValues(FormView $formView): array
|
255 | 256 | continue;
|
256 | 257 | }
|
257 | 258 |
|
| 259 | + // <input type="checkbox"> |
258 | 260 | if (\array_key_exists('checked', $child->vars)) {
|
259 |
| - // special handling for check boxes |
260 | 261 | $values[$name] = $child->vars['checked'] ? $child->vars['value'] : null;
|
261 | 262 | continue;
|
262 | 263 | }
|
263 | 264 |
|
264 |
| - if (\array_key_exists('choices', $child->vars) |
265 |
| - && $child->vars['required'] |
266 |
| - && !$child->vars['disabled'] |
267 |
| - && !$child->vars['value'] |
268 |
| - && (false === $child->vars['placeholder'] || null === $child->vars['placeholder']) |
269 |
| - && !$child->vars['multiple'] |
270 |
| - && !$child->vars['expanded'] |
271 |
| - && $child->vars['choices'] |
| 265 | + // <select> - Simulate browser behavior |
| 266 | + // When no option is selected, browsers send the value of the first |
| 267 | + // option, when the following conditions are met: |
| 268 | + if ( |
| 269 | + \array_key_exists('choices', $child->vars) |
| 270 | + && $child->vars['choices'] // has defined choices |
| 271 | + && $child->vars['required'] // is required |
| 272 | + && !$child->vars['disabled'] // is not disabled |
| 273 | + && '' === $child->vars['value'] // has no value set ("0" can be a value) |
| 274 | + && !array_diff_key( |
| 275 | + /* @see \Symfony\Component\Form\Extension\Core\Type\ChoiceType::buildView() */ |
| 276 | + array_flip(['choices', 'expanded', 'multiple', 'placeholder', 'placeholder_in_choices', 'preferred_choices']), |
| 277 | + $child->vars, |
| 278 | + ) |
| 279 | + && !$child->vars['expanded'] // is a <select> (not a radio/checkbox) |
| 280 | + && !$child->vars['multiple'] // is not multiple |
| 281 | + && !\is_string($child->vars['placeholder']) // has no placeholder (empty string is valid) |
272 | 282 | ) {
|
273 |
| - if (null !== $firstKey = array_key_first($child->vars['choices'])) { |
274 |
| - $values[$name] = $child->vars['choices'][$firstKey]->value ?? null; |
275 |
| - } |
276 |
| - |
| 283 | + $choices = $child->vars['choices']; |
| 284 | + do { |
| 285 | + $choice = $choices[array_key_first($choices)]; |
| 286 | + if (!$choice instanceof ChoiceGroupView) { |
| 287 | + break; |
| 288 | + } |
| 289 | + } while ($choices = $choice->choices); |
| 290 | + |
| 291 | + $values[$name] = $choice?->value; |
277 | 292 | continue;
|
278 | 293 | }
|
279 | 294 |
|
|
0 commit comments