Skip to content

[LiveComponent] Working around issue where FormView is passed to the component #943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2023
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
4 changes: 4 additions & 0 deletions src/Autocomplete/src/Form/ParentEntityAutocompleteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public function finishView(FormView $view, FormInterface $form, array $options)
array_splice($view['autocomplete']->vars['block_prefixes'], -1, 0, 'ux_entity_autocomplete_inner');
// this IS A compound (i.e. has children) field
// however, we only render the child "autocomplete" field. So for rendering, fake NOT compound
// This is a hack and we should check into removing it in the future
$view->vars['compound'] = false;
// the above, unfortunately, can also trick other things that might use
// "compound" for other reasons. This, at least, leaves a hint.
$view->vars['compound_data'] = true;
}

public function configureOptions(OptionsResolver $resolver)
Expand Down
16 changes: 9 additions & 7 deletions src/LiveComponent/src/ComponentWithFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function initializeForm(array $data): array
}

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

return $data;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ private function resetForm(): void
$this->shouldAutoSubmitForm = false;
$this->formInstance = null;
$this->formView = null;
$this->formValues = $this->extractFormValues($this->getForm(), $this->getFormInstance());
$this->formValues = $this->extractFormValues($this->getForm());
}

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

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

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

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

continue;
}
Expand Down
2 changes: 2 additions & 0 deletions src/LiveComponent/tests/Fixtures/Form/ComplexFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
public function finishView(FormView $view, FormInterface $form, array $options)
{
// try to confuse ComponentWithFormTrait
// mimics what autocomplete does
$view->vars['compound'] = false;
$view->vars['compound_data'] = true;
}
}