bolt / boltforms

Bolt 3 Forms extension - Symfony interface and API for Bolt
http://bolt.cm
GNU General Public License v3.0
52 stars 57 forks source link

Feedback Messages inconsistencies with multiple forms #292

Open dadaxr opened 4 years ago

dadaxr commented 4 years ago

The way feedback messages are coded prevent them to be used when there are multiple (bolt) forms on a page.

According to the following use case : There is a first form on my page : the contact form. Then a second form bellow, in the footer : the newsletter form.

If I correctly fill the 2nd form, and submit it, the feedback messages are correctly set in the code ... until the internal lifecycle event listener is triggered : When the src/extensions/vendor/bolt/boltforms/src/Submission/Processor/Redirect.php "process" function is called, the behavior is to send a redirect response (to transform the POST request in a GET request).

The problem lied here : when the new redirect request is handled, the forms are built again, starting with the first form (contact form), and the flash messages which were initially set by the 2nd form are now retrieved for the first form... (everything happen in the src/extensions/vendor/bolt/boltforms/src/Factory/FormContext.php file within the "build" function ).

A possible workarround could be to tied feedback messages to their respective form (using the form name), instead of globaly. We could then have the following structure :

[
"contact_form:debug" : [...],
"contact_form:error" : [...],
"contact_form:info" : [...],
"newsletter_form:debug" : [...],
"newsletter_form:error" : [...],
"newsletter_form:info" : [...],
...
]

the PR should be quite easy to do ... but it's a breaking change (it has an impact on templates).

what do you think ?

jadwigo commented 4 years ago

As far as I remember the feedback messages can be overridden in the configuration for each form, similar to the redirect.

So you should be able to override the messages in the feedback form like this:

newsletter_form:
    notification:
        enabled: true
        debug: false
    feedback:
        error: "there was an error in the newsletter form"
        success: "The newsletter form was successfully submitted"
    fields:
        name:
            type: text
dadaxr commented 4 years ago

Indeed you can defined feedback message by form in the config file.

But the problem is not setting the feedback message. The problem is displaying the correctly defined message next to the right form in the template.

In short : with the current behavior, every form are fetching feedback message from the same variable in session. In theory It should not break because we submit only one form at once ... except when there is a redirect (which is the default case) which break that logic.

dadaxr commented 4 years ago

I can illustrate the problem like that :

given the following code :

{# my twig template #}
{{bolt_form("first_form")}}

{{bolt_form("second_form")}}
first_form:
    notification:
        enabled: true
        debug: false
    feedback:
        success: "The first form was successfully submitted"
     ...

second_form:
    notification:
        enabled: true
        debug: false
    feedback:
        success: "The second form was successfully submitted"
     ...

after the first page load it display something like that :

<form id="first_form">...</form>

<form id="second_form">...</form>

Then if I submit the 2nd form , it will display :

<div class="bolt-message bolt-success">The second form was successfully submitted</div>
<form id="first_form">...</form>

<form id="second_form">...</form>

Instead of :


<form id="first_form">...</form>

<div class="bolt-message bolt-success">The second form was successfully submitted</div>
<form id="second_form">...</form>
bobdenotter commented 4 years ago

the PR should be quite easy to do ... but it's a breaking change (it has an impact on templates).

If you can spare the time to do a PR, that'd be welcome. I don't mind a major version bump for this! :-)

SantinoPetrovic commented 4 years ago

Hi! Anyone came up with a work around for this issue?