EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Error with AssociationField (renderAsEmbeddedForm)

Open john-dufrene-dev opened this issue 4 years ago • 3 comments

Describe the bug With latest version 4.4.3 of EasyAdminBundle when I use Association Field with parameter renderAsEmbeddedForm and OneToOne association I have this error, not sure if it's because I use it bad or because have a bug,

Capture d’écran 2022-11-26 à 23 20 30

Thank's

john-dufrene-dev avatar Nov 26 '22 22:11 john-dufrene-dev

I edit "src/Form/Type/CrudFormType.php" -> "finishView" function and add to the array :

'form_tab' => $form->getConfig()->getAttribute('ea_form_tab'), 'form_panel' => $form->getConfig()->getAttribute('ea_form_panel'),

to move forward while waiting for the correction

uneak avatar Dec 24 '22 03:12 uneak

I didn't dig this issue more than @uneak, but his fix seems to work for me. In case someone needs a more resilient hotfix for their project, here's mine which prevent modifying files from vendor folder.

Add these two files in you project :

FixedCrudFormType.php :

/**
 * This fixes an issue when using AssociationField.renderAsEmbeddedForm on a One-To-One association.
 * This class just inject the missing fields needed to build the embedded form.
 */
class FixedCrudFormType extends CrudFormType
{
    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        parent::finishView($view, $form, $options);
        $view->vars['ea_crud_form'] = array_merge($view->vars['ea_crud_form'], [
            'form_tab' => $form->getConfig()->getAttribute('ea_form_tab'),
            'form_panel' => $form->getConfig()->getAttribute('ea_form_panel'),
        ]);
    }
}

FixedAssociationConfigurator.php :

/**
 * This fixes an issue when using AssociationField.renderAsEmbeddedForm on a One-To-One association.
 * This class must be declared as a service with a low priority to post-process AssociationConfigurator (which handles all the main configuration job) and forces to use FixedCrudFormType as a formType instead of the original CrudFormType.
 */
class FixedAssociationConfigurator implements FieldConfiguratorInterface
{
    public function supports(FieldDto $field, EntityDto $entityDto): bool
    {
        return AssociationField::class === $field->getFieldFqcn();
    }

    public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $context): void
    {
        if (true === $field->getCustomOption(AssociationField::OPTION_RENDER_AS_EMBEDDED_FORM)) {
            $field->setFormType(FixedCrudFormType::class);
        }
    }
}

You can then declare these services :

    App\Core\EasyAdmin\FixedAssociationConfigurator:
        tags:
            - { name: 'ea.field_configurator', priority: -1 }
    App\Core\EasyAdmin\FixedCrudFormType:
        arguments: [ '@form.type_guesser.doctrine' ]
        tags: [ 'form.type' ]

Hope it helps someone until a proper investigation and fix is done.

DayS avatar Jan 16 '23 22:01 DayS

This issue has been automatically closed as part of our end-of-year repository maintenance. As an open-source project with limited resources, we need to focus our efforts on the most current and impactful issues. If this feature or bug is still relevant to you, please create a new issue. For bugs, include a detailed reproducer, and for features, provide a clear explanation of your request. Thank you for your understanding and for supporting our project!

javiereguiluz avatar Dec 31 '24 09:12 javiereguiluz