From e5f7dde3ca20481dde06e419643212c9fcedf472 Mon Sep 17 00:00:00 2001 From: davidv Date: Thu, 22 Mar 2018 18:35:47 +0100 Subject: [PATCH 1/6] Override form_start block to include preserving GET parameters from query when submitting form. --- DependencyInjection/VanioWebExtension.php | 1 + Resources/views/formStartLayout.html.twig | 22 +++++++++++++++++++ .../views/preserveQueryFormFields.html.twig | 5 +++++ Templating/WebExtension.php | 1 + 4 files changed, 29 insertions(+) create mode 100644 Resources/views/formStartLayout.html.twig create mode 100644 Resources/views/preserveQueryFormFields.html.twig diff --git a/DependencyInjection/VanioWebExtension.php b/DependencyInjection/VanioWebExtension.php index 44b198c..f6a4475 100644 --- a/DependencyInjection/VanioWebExtension.php +++ b/DependencyInjection/VanioWebExtension.php @@ -49,6 +49,7 @@ public function load(array $configs, ContainerBuilder $container) ? $container->getParameter('twig.form.resources') : []; $resources[] = '@VanioWeb/formLayout.html.twig'; + $resources[] = '@VanioWeb/formStartLayout.html.twig'; $resources[] = '@VanioWeb/formAttributesLayout.html.twig'; if ($config['recursive_form_label']) { diff --git a/Resources/views/formStartLayout.html.twig b/Resources/views/formStartLayout.html.twig new file mode 100644 index 0000000..43ad6ad --- /dev/null +++ b/Resources/views/formStartLayout.html.twig @@ -0,0 +1,22 @@ +{% block form_start %} + {% set attr = id is not same as(form.vars.id) ? {id: id, novalidate: 'novalidate'} : {novalidate: 'novalidate'} %} + + {%- do form.setMethodRendered() -%} + {% set method = method|upper %} + + {%- if method in ["GET", "POST"] -%} + {% set form_method = method %} + {%- else -%} + {% set form_method = "POST" %} + {%- endif -%} + +
+ + {%- if form_method != method -%} + + {%- endif -%} + + {%- if preserveQuery is defined -%} + {% include '@VanioWeb/preserveQueryFormFields.html.twig' %} + {%- endif -%} +{% endblock %} diff --git a/Resources/views/preserveQueryFormFields.html.twig b/Resources/views/preserveQueryFormFields.html.twig new file mode 100644 index 0000000..1d5b2d5 --- /dev/null +++ b/Resources/views/preserveQueryFormFields.html.twig @@ -0,0 +1,5 @@ +{% set preserveQuery = preserveQuery is iterable ? preserveQuery : [preserveQuery] %} + +{% for parameter in preserveQuery if app.request.query.has(parameter) %} + +{% endfor %} diff --git a/Templating/WebExtension.php b/Templating/WebExtension.php index c150fbe..18daa18 100644 --- a/Templating/WebExtension.php +++ b/Templating/WebExtension.php @@ -59,6 +59,7 @@ class WebExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInt /** @var string[] */ private $themesToAppend = [ + '@VanioWeb/formStartLayout.html.twig', '@VanioWeb/formAttributesLayout.html.twig', '@VanioWeb/recursiveFormLabelLayout.html.twig', '@VanioWeb/collectionWidgetLayout.html.twig', From 6e37a330b8274e0e3ea598a953f68cb94c5d9b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20=C5=A0t=C3=ADpek?= Date: Thu, 22 Mar 2018 18:44:31 +0100 Subject: [PATCH 2/6] Update preserveQueryFormFields.html.twig --- Resources/views/preserveQueryFormFields.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/views/preserveQueryFormFields.html.twig b/Resources/views/preserveQueryFormFields.html.twig index 1d5b2d5..669d132 100644 --- a/Resources/views/preserveQueryFormFields.html.twig +++ b/Resources/views/preserveQueryFormFields.html.twig @@ -1,5 +1,5 @@ {% set preserveQuery = preserveQuery is iterable ? preserveQuery : [preserveQuery] %} {% for parameter in preserveQuery if app.request.query.has(parameter) %} - + {% endfor %} From 8ab5d45e38cf1d0449e59d4e00509a7fb15a9ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20=C5=A0t=C3=ADpek?= Date: Thu, 22 Mar 2018 18:44:53 +0100 Subject: [PATCH 3/6] Update formStartLayout.html.twig --- Resources/views/formStartLayout.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/views/formStartLayout.html.twig b/Resources/views/formStartLayout.html.twig index 43ad6ad..79f047c 100644 --- a/Resources/views/formStartLayout.html.twig +++ b/Resources/views/formStartLayout.html.twig @@ -13,7 +13,7 @@ {%- if form_method != method -%} - + {%- endif -%} {%- if preserveQuery is defined -%} From 1dfa0577066923f8868ff867d51df9717f76f7f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20=C5=A0t=C3=ADpek?= Date: Thu, 22 Mar 2018 18:46:08 +0100 Subject: [PATCH 4/6] Update formStartLayout.html.twig --- Resources/views/formStartLayout.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/views/formStartLayout.html.twig b/Resources/views/formStartLayout.html.twig index 79f047c..c69a6f3 100644 --- a/Resources/views/formStartLayout.html.twig +++ b/Resources/views/formStartLayout.html.twig @@ -13,7 +13,7 @@ {%- if form_method != method -%} - + {%- endif -%} {%- if preserveQuery is defined -%} From 4990ba2728afee73bc3f2694bd02e95491a5f52b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20=C5=A0t=C3=ADpek?= Date: Thu, 22 Mar 2018 18:46:18 +0100 Subject: [PATCH 5/6] Update preserveQueryFormFields.html.twig --- Resources/views/preserveQueryFormFields.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/views/preserveQueryFormFields.html.twig b/Resources/views/preserveQueryFormFields.html.twig index 669d132..f391036 100644 --- a/Resources/views/preserveQueryFormFields.html.twig +++ b/Resources/views/preserveQueryFormFields.html.twig @@ -1,5 +1,5 @@ {% set preserveQuery = preserveQuery is iterable ? preserveQuery : [preserveQuery] %} {% for parameter in preserveQuery if app.request.query.has(parameter) %} - + {% endfor %} From b353259123698439adb3438a7467060b1882c9da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20=C5=A0t=C3=ADpek?= Date: Thu, 22 Mar 2018 19:08:02 +0100 Subject: [PATCH 6/6] Update formStartLayout.html.twig --- Resources/views/formStartLayout.html.twig | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Resources/views/formStartLayout.html.twig b/Resources/views/formStartLayout.html.twig index c69a6f3..d4a4b57 100644 --- a/Resources/views/formStartLayout.html.twig +++ b/Resources/views/formStartLayout.html.twig @@ -1,22 +1,29 @@ {% block form_start %} - {% set attr = id is not same as(form.vars.id) ? {id: id, novalidate: 'novalidate'} : {novalidate: 'novalidate'} %} - {%- do form.setMethodRendered() -%} {% set method = method|upper %} - {%- if method in ["GET", "POST"] -%} + {%- if method in ['GET', 'POST'] -%} {% set form_method = method %} {%- else -%} - {% set form_method = "POST" %} + {% set form_method = 'POST' %} {%- endif -%} - + {% set attr = { + id: id is same as(form.vars.id) ? false : id, + name: name, + method: form_method|lower, + action: action == '' ? false : action, + multipart: multipart ? 'multipart/form-data' : false, + novalidate: true, + } -%} + + {%- if form_method != method -%} {%- endif -%} - {%- if preserveQuery is defined -%} + {% if preserveQuery is defined -%} {% include '@VanioWeb/preserveQueryFormFields.html.twig' %} {%- endif -%} {% endblock %}