From ff9bb0f132bab5b254107569d6146d2b018b92b4 Mon Sep 17 00:00:00 2001 From: Andy Jiang Date: Tue, 11 Apr 2023 09:56:55 -0700 Subject: [PATCH] re-org Form docs (#1104) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - added `/concepts/forms` - added 303 redirect in `POST` form submit code example previous PR: https://github.com/denoland/fresh/pull/1040 --------- Co-authored-by: Matthew Morek Co-authored-by: Luca Casonato Co-authored-by: Séverin Dupouy <32488190+severindupouy@users.noreply.github.com> Co-authored-by: hashrock --- docs/concepts/forms.md | 59 ++++++++++++++++++++++++ docs/getting-started/form-submissions.md | 7 ++- docs/toc.json | 1 + 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 docs/concepts/forms.md diff --git a/docs/concepts/forms.md b/docs/concepts/forms.md new file mode 100644 index 00000000..c972a090 --- /dev/null +++ b/docs/concepts/forms.md @@ -0,0 +1,59 @@ +--- +description: | + Robustly handle user inputs using HTML `
` elements client side, and form + submission handlers server side. +--- + +For stronger resiliency and user experience, Fresh relies on native browser +support for form submissions with the HTML `` element. + +In the browser, a `` submit will send an HTML action (usually `GET` or +`POST`) to the server, which responds with a new page to render. + +## POST request with `multipart/form-data` + +Forms can either submit as a `GET` request with URL search parameter encoding, +or as a `POST` request with `multipart/form-data`. + +This example demonstrates how to handle `multipart/form-data` `` +submissions: + +```tsx +// routes/subscribe.tsx +import { Handlers } from "$fresh/server.ts"; + +export const handler: Handlers = { + async GET(req, ctx) { + return await ctx.render(); + }, + async POST(req, ctx) { + const form = await req.formData(); + const email = form.get("email")?.toString(); + + // Add email to list. + + // Redirect user to thank you page. + const headers = new Headers(); + headers.set("location", "/thanks-for-subscribing"); + return new Response(null, { + status: 303, // See Other + headers, + }); + }, +}; + +export default function Subscribe() { + return ( + <> + + + +
+ + ); +} +``` + +When the user submits the form, Deno will access a specific `email` value from a +`formData()`, add the email to a list, and redirect the user to a thank you +page. diff --git a/docs/getting-started/form-submissions.md b/docs/getting-started/form-submissions.md index ac1a3b94..c566ee68 100644 --- a/docs/getting-started/form-submissions.md +++ b/docs/getting-started/form-submissions.md @@ -18,10 +18,10 @@ JavaScript to make them more interactive. The way forms work in the browser, is that they perform an HTML navigation action when the user submits the form. In most cases this means that when the -form is submitted, a GET or POST request is sent to the server with the form +form is submitted, a `GET` or `POST` request is sent to the server with the form data, which then responds with a new page to render. -Fresh can handle both GET and POST requests through the +Fresh can handle both `GET` and `POST` requests through the [custom handlers][custom-handlers] feature of routes. The handlers can perform any necessary processing on the form data, and then pass data to the `ctx.render()` call to render a new page. @@ -71,6 +71,9 @@ query set as the `q` query parameter in the URL. The `GET` handler will then filter the names array based on the query, and pass it to the page component for rendering. +[Learn more about using forms in Fresh][concepts-forms]. + [custom-handlers]: /docs/getting-started/custom-handlers +[concepts-forms]: /docs/concepts/forms diff --git a/docs/toc.json b/docs/toc.json index 57c28365..5ec37a83 100644 --- a/docs/toc.json +++ b/docs/toc.json @@ -21,6 +21,7 @@ "pages": [ ["architecture", "Architecture"], ["routes", "Routes"], + ["forms", "Forms"], ["islands", "Interactive islands"], ["static-files", "Static files"], ["middleware", "Route middleware"],