-
Notifications
You must be signed in to change notification settings - Fork 22
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
Find an elegant way to show that a redirect stop the method flow #72
Comments
If validation fails, that makes sense to redirect at the condition that you redirect to perhaps another page as here |
Mind that with the suggested syntax, there is no need for comments anymore, it's self explainatory. |
I don't think that the page you redirect to is changing the issue. Whether you redirect to the same page or not, I am saying it should show expressly that the flow is stopped with a You are right though on the fact that if the validation failed, you might want to add some error information. So maybe a throw would be more relevant in that case, something like: @POST
public void add(@RestForm @NotBlank String title, @RestForm @NotBlank String note) {
if(validationFailed()) {
throw new RedirectException(Todos::todos, getValidationErrors());
}
Note todo = new Note();
todo.task = task;
todo.persist();
redirect(Todos::todos);
} |
For sure but I suggest then something like this as example ;-)
|
This syntax is even better
Question:
|
is what: I understand your initial reaction about this, but give the current redirect feature a try, I swear it will grow on you to the point where you never want to use anything else ;) |
Hm, and this one? @POST
public void add(@RestForm @NotBlank String title, @RestForm @NotBlank String note) {
// If validation fails, redirect to the todos page (with errors propagated)
if (!validationFailed()) {
// save the new Todo
Note todo = new Note();
todo.task = task;
todo.persist();
}
// redirect to the todos page
todos();
} |
That's fine too, but naive in most scenarios, because quite often, there's more calls to |
when you have a problem, the solution is to remove the problem! clever boy 😊 Still this is a different coding style and I think allowing the if at the top way of doing things is important.
@FroMage TBH I am fine with the syntax and could live happily ever after with it. Still I think a lot of people might turn around because of this kind of thing. So better give them a way in :) |
Totally agree with @ia3andy
always leads to a redirect. In my application and many others you don't do a redirect in case of an error, but instead just render the page (editBlogEntry) without a redirect. A redirect leads to bad usability. The rule of thumb is: Do a redirect in case of success and just render the page in case of an error |
I disagree with that statement, especially in Renarde, we always redirect back to the form, preserving the values, in case of errors. That's the most useful thing to do in most cases. Why would you say it's bad usability? |
If a user submits a form with invalid data and an error occurs, redirecting to the same form may cause all the input fields to be cleared. This forces the user to re-enter their data, which can be frustrating, especially for complex or long forms. Even though techniques like using flash storage can temporarily hold data, it is only available for the first redirect and can be lost if the user refreshes the page, leading to potential data loss. Best practice:
This pattern of redirecting on success but rendering the form again on failure ensures users don’t lose their input and reduces frustration in cases of validation errors. Remember also: Some application may choose not to use the web-session at all (because of some architecture decisions like scaling issues; function as a service) They can't even use the flash-context. Meaning the use of Renarde wouldn't work at all. |
I didn't know about it, but what @mr-mos proposed seems like a better approach |
This would also solve #192 |
Currently, it works like this:
Some could argue that it is not obvious in a java world that
todos();
is stopping the flow of the method. It would be nice to offer an alternate more declarative way of doing it. Something like 👇 would make me happy :) :The text was updated successfully, but these errors were encountered: