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

form_row block override in form theme does not work for checkbox #515

Closed
emodric opened this issue Mar 21, 2017 · 11 comments
Closed

form_row block override in form theme does not work for checkbox #515

emodric opened this issue Mar 21, 2017 · 11 comments

Comments

@emodric
Copy link

emodric commented Mar 21, 2017

I tried adding an override for form_row block in app/Resources/views/form/layout.html.twig:

{% block form_row %}
    MY OVERRIDE START

    <div>
        {{- form_label(form) -}}
        {{- form_widget(form) -}}
        {{- form_errors(form) -}}
    </div>

    MY OVERRIDE END
{% endblock %}

I also added a boolean property to one of the entities (for example Post), and corresponding checkbox field to PostType.

It turns out that form_row block override is not applied to Checkbox form type, and I couldn't find the reason why.

This is not an issue in Symfony nor an issue with version of PHP. I tested on PHP 5.6, PHP 7.1 and both show this behaviour. I also tested on Sylius (with Symfony 3.2) and it works okay there, so I think it is related to something in this demo.

If this is intended, how do I make the checkbox form type use my form_row override?

Thanks!

@emodric
Copy link
Author

emodric commented Mar 22, 2017

It turns out this is due to the fact that more specific block overrides are used from bootstrap_3_layout.html.twig, specifically, radio_row and checkbox_row.

Is there a way to tell Symfony forms not to use globally defined themes, but only those specified in theme template?

@javiereguiluz
Copy link
Member

I'd say it's not possible, but let's ask to a real Symfony Forms expert: @HeahDude could you confirm or deny this? Thanks!

@HeahDude
Copy link
Contributor

HeahDude commented May 2, 2017

Hello @emodric, you need to override the template when you don't want to use a global one, see http://symfony.com/doc/current/form/form_customization.html#referencing-blocks-from-inside-the-same-template-as-the-form for an example of "picking" some specific blocks from a template.

@emodric
Copy link
Author

emodric commented May 2, 2017

@HeahDude Yes, I'm aware of that, but it would be quite useful to specify in form_theme tag call that you DO NOT want to apply globally defined form themes.

Our usecase is an administration interface for a layout management application built on top of Symfony framework, It can be installed and used on many full stack Symfony apps like eZ Platform, Sylius and even this Symfony Demo app. We have a form theme that skins many forms in the administration interface and we extended from form_div_layout.html.twig to get the basic look & feel from which we could build upon. However, if some themes are defined globally, like bootstrap_3_layout.html.twig here, they override some behaviour, if they have some blocks not available in form_div_layout.html.twig.

We fixed it by overriding the blocks ourselves, but the issue is that we cannot control what themes may be activated globally. Therefore, to build the administration interfaces that are visually completely different from the rest of the app, I think it would be good to have an option to disable usage of global themes per form.

@HeahDude
Copy link
Contributor

HeahDude commented May 2, 2017

@emodric I think the easiest would then to maintain your own template files, because you can't control that actually, and those framework files may evolve in the future and potentially break your app.

@emodric
Copy link
Author

emodric commented May 2, 2017

@HeahDude Yes, sure, but that's a separate issue. Because, even if we maintain our separate files, we would need to define EVERY possible block in our form theme to stop the globally defined themes from interfering.

A simple example:

If I want my admin interface to have a sepecific design for every form row, I would define a form_row block.

However, if a globally defined theme specifies an override for a specific row, for example number_row, it would be applied before my own form_row because it is more specific and it would break my admin design, with no easy way to detect it or disable it, apart from overriding the number_row block myself in my theme.

@HeahDude
Copy link
Contributor

HeahDude commented May 2, 2017

you DO NOT want to apply globally defined form themes

This is a feature that should be added in symfony core.

even if we maintain our separate files, we would need to define EVERY possible block in our form theme to stop the globally defined themes from interfering.

Indeed, but sadly there is no other way for now.

@emodric
Copy link
Author

emodric commented May 2, 2017

This is a feature that should be added in symfony core.

Yes, that is my point ;)

My idea would be to add an only keyword to form_theme tag, similar to include tag:

{% form_theme form with ['common.html.twig', 'form/fields.html.twig'] only %}

If that sounds good, maybe I could work on it to create a PR?

@HeahDude
Copy link
Contributor

HeahDude commented May 2, 2017

only sounds good to me, any PR would be welcome, thanks!

@emodric
Copy link
Author

emodric commented May 2, 2017

@HeahDude PR created at symfony/symfony#22610

@javiereguiluz
Copy link
Member

Let's close this issue because it's not actionable for the Symfony Demo app. Thanks!

fabpot added a commit to symfony/symfony that referenced this issue Oct 13, 2017
…efault themes when rendering a form (emodric)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] [TwigBridge] Added option to disable usage of default themes when rendering a form

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | symfony/symfony-docs#8495

This adds a possibility to use `only` keyword in `form_theme` tag to disable usage of globally defined form themes, e.g.

`{% form_theme form with ['common.html.twig', 'form/fields.html.twig'] only %}`

Otherwise, in order to completely control the rendering of the forms (for example in custom admin interfaces), one would need to use a form theme which has all the possible twig blocks defined to prevent globally defined themes to interfere with the rendering.

`only` keyword is already used when including a Twig template to transfer only the variables which are explicitly defined in the `include` tag, so it seemed natural to use it here too.

This, of course, means that the user will need to manually `use` all of the templates that are required to render the form, including `form_div_layout.html.twig`

This issue is described in details over at Symfony Demo repo: symfony/demo#515

TODO:

- [x] submit changes to the documentation

Commits
-------

e0681f9 [Form] [TwigBridge] Added option to disable usage of default themes when rendering a form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants