I am implementing a project with play 1.2.4, based on documentation the right way to handle validation is:
public static void signUp() {
render();
}
public static void doSignUp(@Required @Valid User user) {
if (validation.hasErrors()) {
params.flash();
validation.keep();
signUp();
}
user.create();
Application.index();
}
But based on samples provided with play, it seems that different approach is used:
public static void signUp() {
render();
}
public static void doSignUp(@Required @Valid User user) {
if (validation.hasErrors()) {
render("@signUp");
}
user.create();
Application.index();
}
For this small example the code difference is small, but in more complex examples it’s not so simple.
Pros and cons I see are:
First approach:
-
Gives nice URLs to user
-
Always redirects after POST, so no confirm problem if user refreshes page
-
Only one method is responsible for filling renderArgs before calling
template -
Compile time verification that signUp method exits if it gets renamed
Second approach:
- Faster, no redirect/round-trip in browser
So what is the best practice? Which approach to use use along the application?
Let me go over your arguments:
First:
The URL can always be fine in Play 1.x. You can use the following:
So the first argument doesn’t matter.
Second:
I think if a user makes a mistake and press F5 or refresh with other technique it’s good if he get the same errors again. If the user should get the possibility to get a clean form, I prefer to have a cancel-button.
Third:
Can’t see the problem with
render("@signUp");Fourth:
Ok, this is an argument but I think it’s weak. Will be false with play 2.0.
So I think both approaches can be good, depending on the situation. Specially if you have a large form, the redirect won’t work. As default I would recommend the second solution.
However, I don’t know how the situation with play 2.0 will be.