From 4f557351ca774162efa19dce15083f426d9586ea Mon Sep 17 00:00:00 2001 From: Ajay Kidave Date: Sat, 19 Oct 2024 21:48:34 -0700 Subject: [PATCH] Send validation messages to overwrite previous values --- internal/app/action/action.go | 18 +++- internal/app/tests/appaction_test.go | 122 ++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 5 deletions(-) diff --git a/internal/app/action/action.go b/internal/app/action/action.go index edb738f..c30f620 100644 --- a/internal/app/action/action.go +++ b/internal/app/action/action.go @@ -248,14 +248,26 @@ func (a *Action) runAction(w http.ResponseWriter, r *http.Request) { return } - // Render the param error messages if any, using HTMX OOB - for paramName, paramError := range paramErrors { + // Render the param error messages, using HTMX OOB + errorMsgs := map[string]string{} + for _, param := range a.params { + // "" error messages have to be sent to overwrite previous values in form UI + if !strings.HasPrefix(param.Name, OPTIONS_PREFIX) { + if paramErrors[param.Name] == nil { + errorMsgs[param.Name] = "" + } else { + errorMsgs[param.Name] = fmt.Sprintf("%s", paramErrors[param.Name]) + } + } + } + + for paramName, paramError := range errorMsgs { tv := struct { Name string Message string }{ Name: paramName, - Message: fmt.Sprintf("%s", paramError), + Message: paramError, } err = a.actionTemplate.ExecuteTemplate(w, "paramError", tv) if err != nil { diff --git a/internal/app/tests/appaction_test.go b/internal/app/tests/appaction_test.go index d4cb2d4..8d2075b 100644 --- a/internal/app/tests/appaction_test.go +++ b/internal/app/tests/appaction_test.go @@ -59,6 +59,13 @@ app = ace.app("testApp",
done
+ +
+
Output
@@ -134,7 +141,14 @@ param("param3", description="param3 description", type=INT, default=10)`, class="text-error mt-1"> param1error
- + +
+
+
done
+ +
+
Report
@@ -240,6 +261,13 @@ app = ace.app("testApp", testutil.AssertEqualsInt(t, "code", 200, response.Code) testutil.AssertStringMatch(t, "match response", `
done
+ +
+
Result
@@ -288,6 +316,13 @@ app = ace.app("testApp",
done
+ +
+
Report
@@ -343,6 +378,13 @@ app = ace.app("testApp", testutil.AssertStringMatch(t, "match response", `
done
+ +
+
Report
@@ -412,6 +454,27 @@ param("param3", description="param3 description", type=INT, default=10)`,
done
+ +
+
+ +
+
+ +
+
Report
@@ -469,6 +532,14 @@ app = ace.app("testApp", testutil.AssertStringMatch(t, "match response", `
done
+ +
+
+
customdata
`, response.Body.String()) // Unset the template @@ -490,6 +561,12 @@ app = ace.app("testApp", a.ServeHTTP(response, request) testutil.AssertEqualsInt(t, "code", 200, response.Code) testutil.AssertStringMatch(t, "match response", `
done
+
+
html/template: "custom" is undefined`, response.Body.String()) } @@ -570,7 +647,27 @@ param("param3", description="param3 description", type=INT, default=10)`, testutil.AssertEqualsInt(t, "code", 200, response.Code) testutil.AssertStringMatch(t, "match response", `
"errormessage" -
`, response.Body.String()) +
+
+
+ +
+
+ +
+
`, response.Body.String()) values = url.Values{ "param1": {"p1val"}, @@ -601,6 +698,27 @@ param("param3", description="param3 description", type=INT, default=10)`, testutil.AssertStringMatch(t, "response", `
done
+ +
+
+ +
+
+ +
+
Report