From 55840499e20ca6abd81d7f3e46107532bac53e78 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Sun, 22 May 2022 21:42:56 +0200
Subject: [PATCH] Fixed #699 - fixed account state warning message

---
 .../budgetmaster/accounts/AccountController.java     | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/accounts/AccountController.java b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/accounts/AccountController.java
index cb3fca9f2..367a6ee19 100644
--- a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/accounts/AccountController.java
+++ b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/accounts/AccountController.java
@@ -37,6 +37,7 @@ public class AccountController extends BaseController
 		public static final String AVAILABLE_ACCOUNT_STATES = "availableAccountStates";
 		public static final String FONTAWESOME_ICONS = "fontawesomeIcons";
 		public static final String ERROR = "error";
+		public static final String NOTIFICATIONS = "notifications";
 	}
 
 	private static class ReturnValues
@@ -166,7 +167,16 @@ public class AccountController extends BaseController
 		if(!isNewAccount && !isAccountStateAllowed)
 		{
 			final String warningMessage = Localization.getString("warning.account.edit.state", Localization.getString(AccountState.FULL_ACCESS.getLocalizationKey()));
-			WebRequestUtils.putNotification(webRequest, new Notification(warningMessage, NotificationType.WARNING));
+			final Notification notification = new Notification(warningMessage, NotificationType.WARNING);
+
+			// Adding an error to the bindingResult will result in an FTL template render instead of a redirect.
+			// For redirects normally WebRequestUtils.putNotification() is used.
+			// Using WebRequestUtils.putNotification() will not work in this case because NotificationAdvice is
+			// executed BEFORE this controller method and therefore the notifications list is empty in the
+			// resulting page.
+			// Quickfix: explicitly set the model attribute "notifications"
+			model.addAttribute(ModelAttributes.NOTIFICATIONS, List.of(notification));
+
 			bindingResult.addError(new FieldError("NewAccount", "state", account.getAccountState(), false, new String[]{"warning.account.edit.state"}, null, null));
 		}
 
-- 
GitLab