You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(docs): Add sample form/function code to the function docs (#31453)
* feat(docs): Add sample form/function code to the function docs
Get people going quick if they want to try adding a form that submits to a function.
* Update docs/docs/how-to/functions/getting-started.md
Co-authored-by: Dustin Schau <[email protected]>
* Add simple server validation
* Run prettier
Co-authored-by: Dustin Schau <[email protected]>
Forms and Functions are often used together. See the [basic form example](https://github.com/gatsbyjs/gatsby-functions-beta/tree/main/examples/basic-form) as well as the [Forms doc page](/docs/how-to/adding-common-features/adding-forms/) which walks you through building a custom form.
126
+
Forms and Functions are often used together. For a working example you can play with locally, see the [form example](https://github.com/gatsbyjs/gatsby-functions-beta/tree/main/examples/basic-form). The [Forms doc page](/docs/how-to/adding-common-features/adding-forms/) is a gentle introduction for building forms in React. Below is sample code for a very simple form that submits to a function that you can use as a basis for building out forms in Gatsby.
127
+
128
+
```js:title=src/api/form.js
129
+
exportdefaultfunctionformHandler(req, res) {
130
+
// req.body has the form values
131
+
console.log(req.body)
132
+
133
+
// Here is where you would validate the form values and
134
+
// do any other actions with it you need (e.g. save it somewhere or
135
+
// trigger an action for the user).
136
+
//
137
+
// e.g.
138
+
139
+
if (!req.body.name) {
140
+
returnres.status(422).json("Name field is required")
0 commit comments