feat: add FormState for re-rendering forms after validation - #121
Merged
Conversation
Tier-2 website-DX: ctx.FormState() returns a *FormState that carries the submitted form values plus per-field validation errors, so a form can be re-rendered with the user's input instead of making them retype. - Value/Values/SetValue — submitted (or pre-filled) values - AddError/Error/Errors/HasError/HasErrors — per-field validation errors The handler builds it, adds errors, and passes it to the view on failure; no per-request template funcs needed. Values are copied from the parsed form, so SetValue does not mutate the request. stdlib only, 100% covered. Verified with go vet, go test -race, gofmt. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Tier-2 website-DX:
ctx.FormState()returns a*FormStatethat holds the submitted form values together with per-field validation errors, so a form can be re-rendered with the user's input after a failed submission (instead of making them retype).API
Value(name),Values(name),SetValue(name, value)(pre-fill, e.g. an edit form)AddError(name, msg),Error(name)(first),Errors(name)(all),HasError(name),HasErrors()Design notes (open to redirection)
isRoute, which still needs that enabler).SetValuenever mutatesctx.Request.Form(tested).ctx.FormValue); usesParseMultipartForm, so it works for urlencoded and multipart text fields.map[string][]string) — a field can carry several messages.Test plan
go vet ./...go test ./...— 100% coverage on all 10FormStatefunctions (incl. the no-mutation guarantee)go test -race ./...gofmtcleanExampleContext_FormState; CLAUDE.md updatedTier-2 status
This leaves
isRouteas the last Tier-2 item — gated on the per-request template-funcs enabler decision.🤖 Generated with Claude Code