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 submit without action/redirect/display #367

Open
VincentFericelli opened this issue Aug 19, 2019 · 5 comments
Open

Form submit without action/redirect/display #367

VincentFericelli opened this issue Aug 19, 2019 · 5 comments
Labels

Comments

@VincentFericelli
Copy link

I have a form embedded in my custom sidebar and I want the user to stay on the same page after the validation. The problem is that the validation cause the form to display itself replacing the whole page.

Here is the markdown 'pages/modules/subscribe/form.fr.md'

---
title: My contact Form
process:
    markdown: true
    twig: true
form:
    name: contact-form
    fields:
        - name: email
          label: Recevoir toutes les nouveautés directement
          autocomplete: on
          placeholder: Votre e-mail
          type: email

    buttons:
        - type: submit
          value: Valider

    process:
        - save:
            filename: newsLetterFR.csv
            extension: csv
            body: "{{ form.value.email|e }}\n"
            operation: add
---

Maybe the problem is how I integrated the form in 'myThemeDerivatedFromQuark/templates/partials/sidebar.html.twig

<div id="sidebar-subscribe">
{% include "embedded_form.html.twig" with { form: forms('contact-form') } %}
</div>

I tried action: "." , action: "" , action: . , action: without success.

I think the problem comes from 'plugins/form/templates/forms/default/form.html.twig' line 20
'{% set action = form.action ?: page.route ~ uri.params %}'

"form.action" is defaulted to "modules/subscribe" by the calling code. I tried to backtrace the problem but failed miserably...

@ulab
Copy link

ulab commented Aug 21, 2019

You might want to ask this on the forum, since it is not really an issue with Grav. Also it is probably more related to the form plugin that has its own repository.

That said, perhaps the How to: Forms in modular pages might help?

I don't have much experience with forms, but have you tried adding a page parameter to the with part of the include, since form.html.twig sets the default action based on the page.route?

@rhukster
Copy link
Member

You should set the action to the route of the page that contains the sidebar as that is the page you want to end up on.

@rhukster rhukster transferred this issue from getgrav/grav Aug 21, 2019
@VincentFericelli
Copy link
Author

I don't know how to get that url, the form is located in a partial template integrated in all the pages of my site.

I tried page.url, but it returns the adress of the generic .md file containing the form.

@rhukster
Copy link
Member

That is a little tricky, actions are tied to forms so having different actions on the same form is not currently possible.

Couple of workaround approaches:

  • Use an Ajax form submission (see learn.getgrav.org for example)
  • Submit to a single page, then redirect back to the origin page.

@NicoHood
Copy link
Contributor

NicoHood commented Jan 9, 2021

You could try the redirect action, with this patch applied: #466
I guess the issue is the modular here, as modular templates set page to the modular child, not the parent. But that is just a guess, I am not sure if I understand the issue correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants