feat(workflow): example stats page with embedded Chart.js, inline form validation errors

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-24 01:59:27 +02:00
co-authored by Claude Sonnet 5.5
parent 16a0bb0de4
commit 00af65613d
36 changed files with 1274 additions and 138 deletions
+59 -24
View File
@@ -1,5 +1,6 @@
#include "App.hpp"
#include "FormUi.hpp"
#include "../fw/Negotiate.hpp"
#include "../fw/Router.hpp"
#include "../fw/StaticFiles.hpp"
@@ -25,6 +26,27 @@ fw::Model user_model() {
age});
}
FormView contact_view() {
FormView v;
v.title = "Contact";
v.action = "/forms/contact";
v.fields = {{"name", "Name", "text", ""},
{"email", "Email", "email", ""},
{"message", "Message", "textarea", ""}};
return v;
}
FormView user_view() {
FormView v;
v.title = "Validate a user";
v.action = "/validate/user";
v.submit = "Validate";
v.fields = {{"username", "Username", "text", "3 to 40 characters"},
{"email", "Email", "email", ""},
{"age", "Age", "number", "whole number, 0 to 150"}};
return v;
}
} // namespace
void register_formats_demo(fw::Router& router, Services& services) {
@@ -67,51 +89,64 @@ void register_formats_demo(fw::Router& router, Services& services) {
router.get("/forms/contact", "contact-get", [env](fw::Request req) {
(void)req;
fw::TemplateCtx ctx;
ctx["title"] = std::string("Contact");
ctx["posted"] = std::string("");
ctx["name"] = std::string("");
ctx["email"] = std::string("");
ctx["message"] = std::string("");
return fw::Response(200, env->render("contact.html", ctx),
FormView view = contact_view();
return fw::Response(200, env->render("form_page.html", view.ctx()),
"text/html; charset=utf-8");
});
router.post("/forms/contact", "contact-post", [env](fw::Request req) {
router.post("/forms/contact", "contact-post", [env](fw::Request req) -> fw::Response {
fw::Form form(contact_model());
fw::TemplateCtx ctx;
ctx["title"] = std::string("Contact");
FormView view = contact_view();
try {
auto clean = form.bind(req);
ctx["posted"] = std::string("true");
ctx["name"] = clean["name"];
ctx["email"] = clean["email"];
ctx["message"] = clean["message"];
if (!wants_html(req))
return fw::JsonResponse("{\"ok\":true,\"name\":\"" +
fw::json_escape(clean["name"]) + "\"}",
200);
view.done = true;
view.notice = "Thanks " + clean["name"] + ", message received.";
} catch (const fw::ValidationError& e) {
ctx["posted"] = std::string("");
auto input = fw::parse_form(req.body);
ctx["name"] = input.count("name") ? input["name"] : "";
ctx["email"] = input.count("email") ? input["email"] : "";
ctx["message"] = input.count("message") ? input["message"] : "";
ctx["error"] = e.to_json();
return fw::Response(422, env->render("contact.html", ctx),
if (!wants_html(req)) return fw::JsonResponse(e.to_json(), 422);
view.values = fw::parse_form(req.body);
view.error = &e;
return fw::Response(422, env->render("form_page.html", view.ctx()),
"text/html; charset=utf-8");
}
return fw::Response(200, env->render("contact.html", ctx),
return fw::Response(200, env->render("form_page.html", view.ctx()),
"text/html; charset=utf-8");
});
router.post("/validate/user", "validate-user", [](fw::Request req) {
router.get("/validate/user", "validate-user-form", [env](fw::Request req) {
(void)req;
FormView view = user_view();
return fw::Response(200, env->render("form_page.html", view.ctx()),
"text/html; charset=utf-8");
});
router.post("/validate/user", "validate-user", [env](fw::Request req) -> fw::Response {
fw::Model model = user_model();
auto input = fw::parse_json_object_flat(req.body);
if (input.empty()) input = fw::parse_form(req.body);
const bool html = req.headers.get("Accept").find("text/html") !=
std::string::npos;
try {
auto clean = model.validate(input);
if (html) {
FormView view = user_view();
view.done = true;
view.notice = "User " + clean["username"] + " is valid.";
return fw::Response(200, env->render("form_page.html", view.ctx()),
"text/html; charset=utf-8");
}
return fw::JsonResponse(
"{\"ok\":true,\"username\":\"" +
fw::json_escape(clean["username"]) + "\"}",
200);
} catch (const fw::ValidationError& e) {
return fw::JsonResponse(e.to_json(), 422);
if (!html) return fw::JsonResponse(e.to_json(), 422);
FormView view = user_view();
view.values = input;
view.error = &e;
return fw::Response(422, env->render("form_page.html", view.ctx()),
"text/html; charset=utf-8");
}
});