Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Form: dynamic form action URL

form

Started by fosil 7 years ago · 4 replies · 2216 views
7 years ago

Hello
I need to dynamically set the action URL of the form.
1) Can I set to insert query string or uri.params automatically?
2) I tried twig in action in the form definition but it will not be done
3) I try to find a specific template and a place in it where you could override, but so far nothing works (there would be nice to add uri.params or anchor)
4) Is it possible to change the action dynamically in the plugin during a custom process action (onFormProcessed)?

Thanks!

7 years ago

ad 3) Here you can use a redirect with the specified form value. https://learn.getgrav.org/16/forms/forms/reference-form-actions#redirect
In the template then something like this:

TWIG
{% extends 'forms/form.html.twig' %}
{% block field %}
  {# next (redirect url) field #}
  {% if field.name == "next" %}
    {% set field = field|merge({'default': "redirect/url/"}) %}
  {% endif %}
{% endblock %}

But beware! If you want to use it like me to redirect to the same page, including uri.params, be sure to remove the "reset" action because it will clear the form data (including, of course, the field that contains the redirection target). Logical, but it took me some time :-)

7 years ago

@fosil, I've been playing a bit with forms...

  • In 'user/plugins/form/templates/forms/default/form.html.twig', on line 103, the action is defined in the following block:
    TWIG
    {% block embed_form_core %}
    name="{{ form.name }}"
    action="{{ action | trim('/', 'right') }}"
    method="{{ method }}"{{ multipart }}
    {% if form.id %}id="{{ form.id }}"{% endif %}
    {% if form.novalidate %}novalidate{% endif %}
    {% if form.keep_alive %}data-grav-keepalive="true"{% endif %}
    {% endblock %}
    
  • In Twig you can access 'uri.query' which returns for the url 'http://localhost/grav/site-dev/typography?greeting=hello' the value 'greeting=hello'
  • When you copy 'user/plugins/form/templates/forms/default/form.html.twig' into your own theme, you might be able to add some logic using 'uri.query' and alter the action attribute to your liking.

Maybe this could lead to a solution...

last edited 12/10/19 by pamtbaau
7 years ago

Yes, that's the solution. I tried in my template (extended from forms / form.html.twig) just overide the embed_form_core block (that would be much more elegant than having the entire code of that original template), but it didn't work for me. Probably because it's embedded in the "embed" tag.

So in the end I used a solution with override block "field" - I think it is clearer because it is clear what exactly had to be adjusted for a specific modification of functionality - and that will be a few months in looking into the code a great advantage :-)

last edited 12/10/19 by fosil
7 years ago

So unfortunately ... It didn't turn out to be the ideal solution. Finally, I really overloaded the template form.html.twig. At the same time, I wrote a issue on the form plugin github to respect the value of the action variable if specified somewhere earlier - this is probably the ideal and most versatile solution.

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 46 1 week ago
Plugins · by Xavier, 4 weeks ago
2 55 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 49 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago