Phase 3 — Alerts (P3-05/06/07) #7
@@ -144,6 +144,20 @@ func (s *Server) decryptChannelConfig(ch store.NotificationChannel, dst any) err
|
||||
return json.Unmarshal(plain, dst)
|
||||
}
|
||||
|
||||
// firstNonEmpty returns the first non-empty (after TrimSpace) value in
|
||||
// vals, or "". Used for fields like `name` that appear once per per-kind
|
||||
// sub-form: only the visible kind's input is filled in, so PostForm.Get
|
||||
// (which returns the first regardless of emptiness) would lose the
|
||||
// actual value when the user edits the second or third kind.
|
||||
func firstNonEmpty(vals []string) string {
|
||||
for _, v := range vals {
|
||||
if strings.TrimSpace(v) != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// formHasValue reports whether vals contains want. Used for hidden+checkbox
|
||||
// pairs (e.g. <input hidden name=x value=0> + <input checkbox name=x value=1>)
|
||||
// where r.PostForm.Get returns the first ("0") even when the checkbox is
|
||||
@@ -162,7 +176,7 @@ func formHasValue(vals []string, want string) bool {
|
||||
func formFromRequest(r *stdhttp.Request) *notificationForm {
|
||||
f := ¬ificationForm{
|
||||
Kind: strings.TrimSpace(r.PostForm.Get("kind")),
|
||||
Name: strings.TrimSpace(r.PostForm.Get("name")),
|
||||
Name: strings.TrimSpace(firstNonEmpty(r.PostForm["name"])),
|
||||
Enabled: formHasValue(r.PostForm["enabled"], "1"),
|
||||
DefaultPriority: strings.TrimSpace(r.PostForm.Get("default_priority")),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user