fix: read 'name' across all per-kind sub-forms when editing channels
The channel form has three inputs all named 'name' (one per kind
section: webhook / ntfy / smtp), but only the visible kind's input
is filled in. PostForm.Get returns the first regardless of
emptiness, so editing an ntfy or smtp channel always read '' from
the (hidden, unfilled) webhook section's name input and rejected
with 'name required'.
Add firstNonEmpty helper that scans the slice for the first
non-blank value. Same flavour of bug as the enabled checkbox fix
in 24eecc1 — both fall out of having multiple inputs share a name
across the per-kind sub-forms.
This commit is contained in:
@@ -144,6 +144,20 @@ func (s *Server) decryptChannelConfig(ch store.NotificationChannel, dst any) err
|
|||||||
return json.Unmarshal(plain, dst)
|
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
|
// 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>)
|
// 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
|
// 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 {
|
func formFromRequest(r *stdhttp.Request) *notificationForm {
|
||||||
f := ¬ificationForm{
|
f := ¬ificationForm{
|
||||||
Kind: strings.TrimSpace(r.PostForm.Get("kind")),
|
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"),
|
Enabled: formHasValue(r.PostForm["enabled"], "1"),
|
||||||
DefaultPriority: strings.TrimSpace(r.PostForm.Get("default_priority")),
|
DefaultPriority: strings.TrimSpace(r.PostForm.Get("default_priority")),
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user