From af6f5a6d7c5faf4428c287a76bb045fb633314d7 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Sat, 23 Jul 2022 16:18:47 +0200
Subject: [PATCH] #677 - perform keyword check for transaction names in
 templates too

---
 .../src/main/resources/static/js/transactions.js       | 10 ++++++----
 .../main/resources/templates/templates/newTemplate.ftl |  4 +++-
 .../templates/transactions/newTransactionNormal.ftl    |  2 +-
 .../templates/transactions/newTransactionTransfer.ftl  |  2 +-
 4 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/BudgetMasterServer/src/main/resources/static/js/transactions.js b/BudgetMasterServer/src/main/resources/static/js/transactions.js
index 2d4f40714..46d684333 100644
--- a/BudgetMasterServer/src/main/resources/static/js/transactions.js
+++ b/BudgetMasterServer/src/main/resources/static/js/transactions.js
@@ -524,19 +524,21 @@ function openKeywordWarningModal(htmlData)
     {
         $(modalID).modal('close');
 
+        let allowEmptyAmount = document.getElementById('template-name') !== null;
+
         // rebind onsubmit function to skip keyword check once
-        document.getElementsByName('NewTransaction')[0].onsubmit = function()
+        document.getElementById('mainForm').onsubmit = function()
         {
-            return validateForm(false, true);
+            return validateForm(allowEmptyAmount, true);
         };
 
         // TODO differentiate between user clicked button "save" or "save and continue" before
         document.getElementById('button-save-transaction').click();
 
         // reset onsubmit function in case user edits transaction name too after fixing validation errors
-        document.getElementsByName('NewTransaction')[0].onsubmit = function()
+        document.getElementById('mainForm').onsubmit = function()
         {
-            return validateForm(false, false);
+            return validateForm(allowEmptyAmount, false);
         };
     });
 }
diff --git a/BudgetMasterServer/src/main/resources/templates/templates/newTemplate.ftl b/BudgetMasterServer/src/main/resources/templates/templates/newTemplate.ftl
index 853a81a73..c0d5c73e2 100644
--- a/BudgetMasterServer/src/main/resources/templates/templates/newTemplate.ftl
+++ b/BudgetMasterServer/src/main/resources/templates/templates/newTemplate.ftl
@@ -36,7 +36,7 @@
                 <@header.content>
                     <div class="container">
                     <#import "../helpers/validation.ftl" as validation>
-                    <form name="NewTemplate" action="<@s.url '/templates/newTemplate'/>" method="post" onsubmit="return validateForm(true)">
+                    <form id="mainForm" name="NewTemplate" action="<@s.url '/templates/newTemplate'/>" method="post" onsubmit="return validateForm(true)">
                         <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                         <input type="hidden" name="ID" value="<#if template.getID()??>${template.getID()?c}</#if>">
                         <input type="hidden" name="templateGroup" value="<#if template.getTemplateGroup()??>${template.getTemplateGroup().getID()?c}</#if>">
@@ -89,6 +89,8 @@
                         <#-- buttons -->
                         <@newTransactionMacros.buttons cancelURL="/templates" includeContinueButton=false/>
                     </form>
+
+                    <div id="transactionNameKeywordWarningModalContainer"></div>
                 </div>
                 </@header.content>
             </div>
diff --git a/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionNormal.ftl b/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionNormal.ftl
index a55e6a074..cf77495d3 100644
--- a/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionNormal.ftl
+++ b/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionNormal.ftl
@@ -33,7 +33,7 @@
                 <@header.content>
                     <div class="container">
                         <#import "../helpers/validation.ftl" as validation>
-                        <form name="NewTransaction" action="<@s.url '/transactions/newTransaction'/>" method="post" onsubmit="return validateForm()">
+                        <form id="mainForm" name="NewTransaction" action="<@s.url '/transactions/newTransaction'/>" method="post" onsubmit="return validateForm()">
                             <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                             <!-- only set ID for transactions not templates, otherwise the input is filled with the template ID and saving the transaction
                             may then override an existing transactions if the ID is also already used in transactions table -->
diff --git a/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionTransfer.ftl b/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionTransfer.ftl
index 73a7426f1..6c9560c74 100644
--- a/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionTransfer.ftl
+++ b/BudgetMasterServer/src/main/resources/templates/transactions/newTransactionTransfer.ftl
@@ -33,7 +33,7 @@
                 <@header.content>
                     <div class="container">
                         <#import "../helpers/validation.ftl" as validation>
-                        <form name="NewTransaction" action="<@s.url '/transactions/newTransaction'/>" method="post" onsubmit="return validateForm()">
+                        <form id="mainForm" name="NewTransaction" action="<@s.url '/transactions/newTransaction'/>" method="post" onsubmit="return validateForm()">
                             <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                             <!-- only set ID for transactions not templates, otherwise the input is filled with the template ID and saving the transaction
                             may then override an existing transactions if the ID is also already used in transactions table -->
-- 
GitLab