<form action="/team_name_url/" method="post">
<label for="team_name">Enter name: </label>
<input
id="team_name"
type="text"
name="name_field"
value="Default name for team." />
<input type="submit" value="OK" />
</form>Express Tutorial Part 6: Working with forms / notebook
Express Tutorial Part 6: Working with forms
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
import { body, validationResult } from "express-validator"; [
// …
body("name", "Empty name").trim().isLength({ min: 1 }).escape(),
// …
]; [
// …
body("age", "Invalid age")
.optional({ values: "falsy" })
.isISO8601()
.toDate(),
// …
]; [
// …
body("name")
.trim()
.isLength({ min: 1 })
.withMessage("Name empty.")
.isAlpha()
.withMessage("Name must be alphabet letters."),
// …
]; async (req, res, next) => {
// Extract the validation errors from a request.
const errors = validationResult(req);
if (!errors.isEmpty()) {
// There are errors. Render form again with sanitized values/errors messages.
// Error messages can be returned in an array using `errors.array()`.
} else {
// Data from form is valid.
}
};// GET request for creating a Genre. NOTE This must come before route that displays Genre (uses id).
router.get("/genre/create", genreController.genreCreateGet);
// POST request for creating Genre.
router.post("/genre/create", genreController.genreCreatePost);