Skip to content
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

[TwigBridge] Form option is settable only for first created form #45229

Closed
maxkain opened this issue Jan 28, 2022 · 5 comments
Closed

[TwigBridge] Form option is settable only for first created form #45229

maxkain opened this issue Jan 28, 2022 · 5 comments

Comments

@maxkain
Copy link

maxkain commented Jan 28, 2022

Symfony version(s) affected

5.4.2

Description

The problem appears when running symfony as a daemon with swoole web server. The option block_prefix is settable once. In other requests it is impossible to set this option to another value. Twig renders form as the block_prefix is not changed, although each time new form instance is created.

How to reproduce

Example is from EasyCorp\Bundle\EasyAdminBundle\Factory:

$this->symfonyFormFactory->createNamedBuilder($entityDto->getName(), CrudFormType::class, $entityDto->getInstance(), $formOptions->all());

If set first parameter to something random value the problem disappears.

And in controller set option like this:

if ($someCondition) {
    $field->getAsDto()->setFormTypeOption('block_prefix',  'some_prefix');
}

Possible Solution

I have decorated twig.form.engine (Symfony\Bridge\Twig\Form\TwigRendererEngine) with function, which is called on kernel.request event:

    public function init()
    {
        $this->resources = [];
    }

It works, but I am not sure it is the right solution.

Additional Context

No response

@xabbuh
Copy link
Member

xabbuh commented Feb 18, 2022

Can you investigate which properties would need to be reset after handling a request?

@maxkain
Copy link
Author

maxkain commented Feb 18, 2022

Can you investigate which properties would need to be reset after handling a request?

I needed to reset only block_prefix property. But I think all properties should be reset.

@xabbuh
Copy link
Member

xabbuh commented Apr 1, 2022

@maxkain Can you try out #45909?

@maxkain
Copy link
Author

maxkain commented Apr 1, 2022

@maxkain Can you try out #45909?

if this works:
$this->resources = [];
I think this will work too:
$this->defaultThemes = [];
$this->themes = [];
$this->useDefaultThemes = [];
$this->resources = [];
$this->resourceHierarchyLevels = [];

@nicolas-grekas
Copy link
Member

It'd still be great if you could confirm please, we never know how bugs happen :)

@fabpot fabpot closed this as completed Apr 1, 2022
fabpot added a commit that referenced this issue Apr 1, 2022
… requests (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form][TwigBundle] reset Twig form theme resources between requests

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #45229
| License       | MIT
| Doc PR        |

Commits
-------

873a86e reset Twig form theme resources between requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants