fix(web): prefer form redirect and soft-default now_if_empty

Desk 文案 cancel links stamp redirect so POST leaves the compose page; empty date fields can opt into 空则现在 without blocking custom values.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-18 12:58:24 +08:00
1 parent 3f96592c99
commit 4b7643d833
5 files changed
+56 -9

No files matched your search

+13 -1
View File
@@ -69,6 +69,8 @@ func SetRules(formBag map[string]any, rules any) map[string]any {
}
// SetLabels stamps localized submit / cancel button copy (and optional cancel href).
// When cancelHref is set, also stamps redirect so successful POST lands on the list
// (or other cancel target) instead of the composing page's _mq_return path.
func SetLabels(formBag map[string]any, submit, cancel, cancelHref string) map[string]any {
out := cloneMap(formBag)
if s := strings.TrimSpace(submit); s != "" {
@@ -79,6 +81,7 @@ func SetLabels(formBag map[string]any, submit, cancel, cancelHref string) map[st
}
if s := strings.TrimSpace(cancelHref); s != "" {
out["cancel_href"] = s
out["redirect"] = s
}
return out
}
@@ -725,7 +728,16 @@ func RenderBodyCtx(formBag map[string]any, formID string, data, errors any, csrf
if csrf != "" {
fmt.Fprintf(&body, `<input type="hidden" name="_csrf" value="%s"/>`, esc(csrf))
}
if action == "update" && rowID != "" {
hasIDField := false
for _, f := range fields {
if fm, ok := f.(map[string]any); ok {
if strOf(fm, "name", "") == "id" && IsClientSource(strOf(fm, "source", "client")) {
hasIDField = true
break
}
}
}
if action == "update" && rowID != "" && !hasIDField {
fmt.Fprintf(&body, `<input type="hidden" name="id" value="%s"/>`, esc(rowID))
}
if ctx != nil {
+3
View File
@@ -75,6 +75,9 @@ func TestRenderMarkdownAndLabels(t *testing.T) {
map[string]any{"字段": "content", "标签": "正文", "类型": "markdown", "必填": true},
})
f = form.SetLabels(f, "发布文章", "返回列表", "/desk/posts")
if f["redirect"] != "/desk/posts" || f["cancel_href"] != "/desk/posts" {
t.Fatalf("labels should stamp redirect/cancel_href, got %#v %#v", f["redirect"], f["cancel_href"])
}
html := form.Render(f, "post", nil, nil, "")
for _, want := range []string{
`data-mq-field="markdown"`,
+20 -7
View File
@@ -18,6 +18,8 @@ func normalizeSource(raw string) string {
return "session.username"
case "now", "utc.now", "server.now", "时间", "utc":
return "now"
case "now_if_empty", "空则现在", "now?":
return "now_if_empty"
default:
return s
}
@@ -25,7 +27,8 @@ func normalizeSource(raw string) string {
// IsClientSource reports whether the field is filled by the browser.
func IsClientSource(source string) bool {
return normalizeSource(source) == "client"
src := normalizeSource(source)
return src == "client" || src == "now_if_empty"
}
// RequestContext carries session user and route params for form source stamping
@@ -39,7 +42,8 @@ type RequestContext struct {
}
// ApplySources strips client-supplied values for non-client fields, then stamps
// from ctx. Always overwrites server-owned keys.
// from ctx. Always overwrites server-owned keys. `now_if_empty` keeps a non-empty
// client value and only stamps UTC now when the field was left blank.
func ApplySources(formBag map[string]any, data map[string]any, ctx *RequestContext) {
if data == nil {
return
@@ -57,12 +61,21 @@ func ApplySources(formBag map[string]any, data map[string]any, ctx *RequestConte
continue
}
src := normalizeSource(strOf(fm, "source", "client"))
if src == "client" {
switch src {
case "client":
continue
}
delete(data, name)
if v, ok := resolveSource(src, ctx); ok {
data[name] = v
case "now_if_empty":
if data[name] != nil && strings.TrimSpace(cellStr(data[name])) != "" {
continue
}
if v, ok := resolveSource("now", ctx); ok {
data[name] = v
}
default:
delete(data, name)
if v, ok := resolveSource(src, ctx); ok {
data[name] = v
}
}
}
}
+17
View File
@@ -32,6 +32,23 @@ func TestApplySourcesOverwritesClient(t *testing.T) {
}
}
func TestApplySourcesNowIfEmpty(t *testing.T) {
frm := SetFields(New("news", "insert", "n"), []any{
map[string]any{"字段": "title", "标签": "t", "类型": "text", "必填": true},
map[string]any{"字段": "published_at", "标签": "d", "类型": "text", "来源": "空则现在"},
})
empty := map[string]any{"title": "x", "published_at": ""}
ApplySources(frm, empty, &RequestContext{})
if empty["published_at"] == nil || empty["published_at"] == "" {
t.Fatalf("expected stamp, got %#v", empty["published_at"])
}
kept := map[string]any{"title": "x", "published_at": "2020-01-02"}
ApplySources(frm, kept, &RequestContext{})
if kept["published_at"] != "2020-01-02" {
t.Fatalf("should keep client date, got %#v", kept["published_at"])
}
}
func TestRenderSkipsServerSources(t *testing.T) {
frm := SetFields(New("comments", "insert", "c"), []any{
map[string]any{"字段": "body", "标签": "Body", "类型": "textarea", "必填": true},
+3 -1
View File
@@ -504,8 +504,10 @@ func (st *state) handleFormPost(w http.ResponseWriter, r *http.Request) {
return
}
if ok, _ := res["ok"].(bool); ok {
// Prefer an explicit form redirect (e.g. cancel_href list URL) over the
// composing page path stamped as _mq_return.
redir := strOpt(res, "redirect", "/")
if ctx.ReturnPath != "" {
if (redir == "" || redir == "/") && ctx.ReturnPath != "" {
redir = ctx.ReturnPath
}
http.Redirect(w, r, redir, http.StatusSeeOther)