Skip to content

Commit

Permalink
Send validation messages to overwrite previous values
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Oct 20, 2024
1 parent 4c6ee56 commit 4f55735
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 5 deletions.
18 changes: 15 additions & 3 deletions internal/app/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
122 changes: 120 additions & 2 deletions internal/app/tests/appaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ app = ace.app("testApp",
<div class="text-lg text-bold">
done
</div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
<div class="divider text-lg text-secondary">Output</div>
Expand Down Expand Up @@ -134,7 +141,14 @@ param("param3", description="param3 description", type=INT, default=10)`,
class="text-error mt-1">
param1error
</div>
<div
id="param_param2_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div
id="param_param3_error"
hx-swap-oob="true"
Expand Down Expand Up @@ -187,6 +201,13 @@ app = ace.app("testApp",
testutil.AssertEqualsInt(t, "code", 200, response.Code)
testutil.AssertStringMatch(t, "match response", `
<div class="text-lg text-bold"> done </div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
<div class="divider text-lg text-secondary">Report</div>
Expand Down Expand Up @@ -240,6 +261,13 @@ app = ace.app("testApp",
testutil.AssertEqualsInt(t, "code", 200, response.Code)
testutil.AssertStringMatch(t, "match response", `
<div class="text-lg text-bold"> done </div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
<div class="divider text-lg text-secondary">Result</div>
Expand Down Expand Up @@ -288,6 +316,13 @@ app = ace.app("testApp",
<div class="text-lg text-bold">
done
</div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
<div class="divider text-lg text-secondary">Report</div>
Expand Down Expand Up @@ -343,6 +378,13 @@ app = ace.app("testApp",
testutil.AssertStringMatch(t, "match response", `<div class="text-lg text-bold">
done
</div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
<div class="divider text-lg text-secondary">Report</div>
Expand Down Expand Up @@ -412,6 +454,27 @@ param("param3", description="param3 description", type=INT, default=10)`,
<div class="text-lg text-bold">
done
</div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div
id="param_param2_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div
id="param_param3_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
<div class="divider text-lg text-secondary">Report</div>
Expand Down Expand Up @@ -469,6 +532,14 @@ app = ace.app("testApp",
testutil.AssertStringMatch(t, "match response", `<div class="text-lg text-bold">
done
</div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML"> customdata </div>`, response.Body.String())

// Unset the template
Expand All @@ -490,6 +561,12 @@ app = ace.app("testApp",
a.ServeHTTP(response, request)
testutil.AssertEqualsInt(t, "code", 200, response.Code)
testutil.AssertStringMatch(t, "match response", `<div class="text-lg text-bold"> done </div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML"> </div>html/template: "custom" is undefined`, response.Body.String())
}

Expand Down Expand Up @@ -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", `<div class="text-lg text-bold">
&#34;errormessage&#34;
</div>`, response.Body.String())
</div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div
id="param_param2_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div
id="param_param3_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>`, response.Body.String())

values = url.Values{
"param1": {"p1val"},
Expand Down Expand Up @@ -601,6 +698,27 @@ param("param3", description="param3 description", type=INT, default=10)`,
testutil.AssertStringMatch(t, "response", `<div class="text-lg text-bold">
done
</div>
<div
id="param_param1_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div
id="param_param2_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div
id="param_param3_error"
hx-swap-oob="true"
hx-swap="outerHTML"
class="text-error mt-1">
</div>
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
<div class="divider text-lg text-secondary">Report</div>
Expand Down

0 comments on commit 4f55735

Please sign in to comment.