mzur/kirby-uniform

Without redirection errors accumulate over time when form is submitted multiple times

Closed this issue · 3 comments

bezin commented

Hi there,

I like your plugin very much when it comes to form handling with Kirby. Recently I run into some unexpected behaviour.

Background
I submit my forms with JavaScript, but return a fully server-side-rendered form with error messages and all and replace the whole form with the updated one. Hence, I set $form->withoutRedirect() to avoid one more request. However, due to the way form data is plucked from the session, this leads to an interesting behaviour: the error messages are accumulated over time.

To reproduce

  1. Create a simple form. Make sure you output error messages in the front-end
  2. Set $form->withoutRedirect()
  3. Enter data that fails your validation and submit form multiple times
  4. The same error message(s) will acumulate over time.

I would expect that when I validate my form, the outcome is always the same, no matter the redirect state.

Why does this not happen with redirection?
Because data and errors are written to the session in the original request. The the server sends the redirect response. The client sends another request to the new location and data and errors are plucked from the session. Hence, when the user submits the form again, the session state is blank.

Workaround
I know I can work around this issue when I also set $form->withoutFlashing() and then use <?= esc($form->data($key)) ?> rather than $form->old($key)

Happy to hear your thoughts on this :-)

Cheerio

mzur commented

The Post-Redirect-Get pattern is the fundamental mechanism of kirby-form on which Uniform is based. So it's not surprising if something like this happens when the redirect is disabled. The AJAX example in the docs also includes withoutFlashing which solves this issue, but in a "real" AJAX scenario you retain the form data, too. So your use case is somewhat unusual. I tend to say that you should just stick with withoutFlashing and $form->data() instead of $form->old() (no need to use esc() with the most recent version by the way: mzur/kirby-form#6). What do you say? Do you suggest a changed behavior of Uniform or some note in the docs?

mzur commented

Please reopen if there is anything else to discuss.

bezin commented

@mzur I just thought about this issue the other day and that you made some valid points but I never responsed to that 🙈 All set here, sorry about that!