Form Authentication Error: Rendering or Redirecting?

There are two different ways to handle actions with validation errors in the documentation for the playback platform:

  • Redirect to index (documentation verification section):
if (validation.hasErrors()) { params.flash(); // add http parameters to the flash scope validation.keep(); // keep the errors for the next request index(); } 
  • Mark the form template (in the tutorial):
  if (validation.hasErrors()) { render("@form", post); } 

What is the best way to do this? Send or forward?

+6
source share
3 answers

There are pros and cons for both approaches.

My personal preference is to use the redirect approach, and the reason is because the browser URL correctly indicates where you are in the application. For example, if your index page triggers a login action, if you do not use redirects, then the browser URL will show the login when you are actually on the index page with errors.

But there is a problem with this approach. To receive redirection and save validation error messages, you need to use validation.keep() and params.flash() (see here for more details) so that the validation is saved for the next request. This is achieved behind the scenes by storing validation errors and user-entered parameters in a cookie (to ensure that Play remains stateless). As you probably know, the HTTP specification limits Cookies to 4Kb, so there is a risk that if you enter data larger than 4Kb (which is possible for forms in which you enter large chunks of text), your Cookie will overflow and you will lose data.

The original method in which Play was used was a redirect method, but the second approach was specifically added to the documentation as a workaround due to the cookie restriction.

The third, and potentially cleaner option is to perform client-side validation using javascript, which invokes some server logic using AJAX. An example of this can be seen in the 10th example of the sample application samples-and-test/validation .

+7
source

I would prefer to re-create the form template because I think redirecting was not useful in such a situation. If the user presses F5, it’s still good that he sends the same data again. The only problem the user is facing is to get the cancel operation.

To avoid the URL problem mentioned by COdemwnci, ​​you can use the following trick in .conf routes

 GET /customer/edit CustomerController.load() POST /customer/edit CustomerController.save() 

Then you always have a clean URL.

+2
source

I also redirect to have clean urls. However, switching from one option to another often requires more than storing the test results and http parameters, namely taking care of all other rendering arguments.

A recent example of my form validation and address correction:

before

 public static void step2(@Valid Address address) { AddressResult result = ....; // perform address check if (result.isCorrected()) { validation.addError("address.streetAndHouseNumber", "Street has been corrected."); address.streetAndHouseNumber = result.getStreet(); render("step1.html", address); } ... // continue } 

after

 public static void step2(@Valid Address address) { AddressResult result = ....; // perform address check if (result.isCorrected()) { validation.addError("address.streetAndHouseNumber", "Street has been corrected."); // ----- note this line -------- params.put("address.streetAndHouseNumber", result.getStreet()); // ----------------------------- params.flash(); validation.keep(); step1(); } ... // continue } 

While local range variables can be easily populated in option 2, we need to update the http resp parameters. write directly in the flash area.

Anyone who has the right abstract level to easily switch between the two options?

+1
source

Source: https://habr.com/ru/post/890336/


All Articles