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

Feature/discussion: generic approach to add controllers from FormType classes #137

Open
zmitic opened this issue Jul 21, 2021 · 0 comments

Comments

@zmitic
Copy link

zmitic commented Jul 21, 2021

I find Stimulus great to improve forms. There is just one problem that I may not know how to solve so here comes an example:

My root form has controller unload that will ask user to confirm turbo:visit if FormData has been changed. And it works but the code is a problem:

My theme.twig
{% use 'bootstrap_4_layout.html.twig' %}

 {% block attributes %}

     {%- if form is rootform and (not unload_rendered is defined) -%}
         {{ stimulus_controller('unload') }}
         {{ stimulus_action({
             'unload':
             {
                 'turbo:submit-start': 'leavingPage',
                 'change': 'onChange',
                 'turbo:click@window->unload': 'leavingPage',
                 'turbo:before-visit@window': 'leavingPage',
                 'beforeunload@window': 'leavingPage',
             }
         }) }}

     {%- endif %}

     {{- parent() -}}

 {% endblock %}

{%- block widget_container_attributes -%}

    {% if form is rootform and (not unload_rendered is defined) %}
        {% set unload_rendered = true %}
    {% endif %}

    {{- parent() -}}
{%- endblock widget_container_attributes -%}

unload_rendered is used to prevent duplicate controllers.

The problem here is that if I wanted to use bootstrap_4_horizontal_layout.html.twig instead, I would need to create another file (am I wrong about this?).

It becomes an even bigger problem for reusable bundles or FormExtensions that want to attach their own controllers (like having 2-3 of them, each with its own logic).

It would have been much easier if the same can be provided via PHP.


The idea

FormView::vars could get another array property called stimulus_controllers.

Then in Twig, template block widget_attributes would read and render them unescaped (this is the biggest issue I had with attributes approach).

End use

$builder->add('firstName', TextType::class, [
    'controller' => 'my-validation'
]);

which would render

<input data-controller="my-validation" ... />

If there was a registered FormExtension for TextType that add its own always-added controller:

<input data-controller="my-validation always-added" ... />

FormView example:

public function buildView(FormView $view, FormInterface $form, array $options): void
{
    $view->vars['stimulus_controllers']['my-controller'] = ['onVisit' => 'turbo:visit@window'];
}

WDYT? Does the idea even make sense?

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

1 participant