Skip to content

Commit 67bc0ea

Browse files
committed
[LiveComponent] Working around issue where FormView is passed to the component
1 parent afe8f2d commit 67bc0ea

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/Autocomplete/src/Form/ParentEntityAutocompleteType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ public function finishView(FormView $view, FormInterface $form, array $options)
5656
array_splice($view['autocomplete']->vars['block_prefixes'], -1, 0, 'ux_entity_autocomplete_inner');
5757
// this IS A compound (i.e. has children) field
5858
// however, we only render the child "autocomplete" field. So for rendering, fake NOT compound
59+
// This is a hack and we should check into removing it in the future
5960
$view->vars['compound'] = false;
61+
// the above, unfortunately, can also trick other things that might use
62+
// "compound" for other reasons. This, at least, leaves a hint.
63+
$view->vars['compound_data'] = true;
6064
}
6165

6266
public function configureOptions(OptionsResolver $resolver)

src/LiveComponent/src/ComponentWithFormTrait.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function initializeForm(array $data): array
9292
}
9393

9494
// set the formValues from the initial form view's data
95-
$this->formValues = $this->extractFormValues($this->getForm(), $this->getFormInstance());
95+
$this->formValues = $this->extractFormValues($this->getForm());
9696

9797
return $data;
9898
}
@@ -145,7 +145,7 @@ private function resetForm(): void
145145
$this->shouldAutoSubmitForm = false;
146146
$this->formInstance = null;
147147
$this->formView = null;
148-
$this->formValues = $this->extractFormValues($this->getForm(), $this->getFormInstance());
148+
$this->formValues = $this->extractFormValues($this->getForm());
149149
}
150150

151151
private function submitForm(bool $validateAll = true): void
@@ -172,7 +172,7 @@ private function submitForm(bool $validateAll = true): void
172172

173173
// re-extract the "view" values in case the submitted data
174174
// changed the underlying data or structure of the form
175-
$this->formValues = $this->extractFormValues($this->getForm(), $form);
175+
$this->formValues = $this->extractFormValues($this->getForm());
176176

177177
// remove any validatedFields that do not exist in data anymore
178178
$this->validatedFields = LiveFormUtility::removePathsNotInData(
@@ -238,7 +238,7 @@ private function getDataModelValue(): ?string
238238
* frontend, and it's meant to equal the raw POST data that would
239239
* be sent if the form were submitted without modification.
240240
*/
241-
private function extractFormValues(FormView $formView, FormInterface $form): array
241+
private function extractFormValues(FormView $formView): array
242242
{
243243
$values = [];
244244

@@ -250,10 +250,12 @@ private function extractFormValues(FormView $formView, FormInterface $form): arr
250250
// is already correct. For example, an expanded ChoiceType with
251251
// options "text" and "phone" would already have a value in the format
252252
// ["text"] (assuming "text" is checked and "phone" is not).
253-
//
254-
$isCompound = $form->has($name) && $form->get($name)->getConfig()->getOption('compound', false);
253+
// "compound" is how we know if a field holds children. The extra
254+
// "compound_data" is a special flag to workaround the fact that
255+
// the "autocomplete" library fakes their compound fake incorrectly.
256+
$isCompound = $child->vars['compound_data'] ?? $child->vars['compound'] ?? false;
255257
if ($isCompound && !($child->vars['expanded'] ?? false)) {
256-
$values[$name] = $this->extractFormValues($child, $form->get($name));
258+
$values[$name] = $this->extractFormValues($child);
257259

258260
continue;
259261
}

src/LiveComponent/tests/Fixtures/Form/ComplexFieldType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
1919
public function finishView(FormView $view, FormInterface $form, array $options)
2020
{
2121
// try to confuse ComponentWithFormTrait
22+
// mimics what autocomplete does
2223
$view->vars['compound'] = false;
24+
$view->vars['compound_data'] = true;
2325
}
2426
}

0 commit comments

Comments
 (0)