0

I'm using express-validator in a node app. All 4 of my form fields are returning validation errors ("A name is required" and so on). When I console.log the errors variable, all values are blank:

errors: [{value: '', msg: 'A name is required.', param: 'name', location: 'body'},...

Feedback form:

 <form class="feedback-form" method="POST" action="/feedback">
          <div class="form-group">
            <label for="feedback-form-name">Name</label>
            <input
              type="text"
              class="form-control"
              id="feedback-form-name"
              name="name"
              placeholder="Enter your name"
            />
          </div>
          <div class="form-group">
            <label for="feedback-form-email">E-Mail</label>
            <input
              type="text"
              class="form-control"
              id="feedback-form-email"
              name="email"
              placeholder="Enter your email address"
            />
          </div>
          <div class="form-group">
            <label for="feedback-form-title">Title</label>
            <input
              type="text"
              class="form-control"
              id="feedback-form-title"
              name="title"
              placeholder="Title of your feedback"
            />
          </div>
          <div class="form-group">
            <label for="feedback-form-message">Message</label>
            <textarea
              type="text"
              placeholder="Enter your message, then hit the submit button"
              class="form-control"
              name="message"
              id="feedback-form-message"
              rows="6"
            ></textarea>
          </div>
          <button type="submit" class="btn btn-secondary float-right">Submit</button>
        </form>

And my router:

router.post(
    "/",
    [
      check("name").trim().isLength({ min: 3 }).escape().withMessage("A name is required."),
      check("email").trim().isEmail().normalizeEmail().withMessage("A valid e-mail is required."),
      check("title").trim().isLength({ min: 3 }).withMessage("A valid title is required."),
      check("message").trim().isLength({ min: 3 }).withMessage("A valid message is required."),
    ],
    (request, response) => {
      const errors = validationResult(request);
      console.log(errors);
      if (!errors.isEmpty()) {
        request.session.feedback = {
          errors: errors.array(),
        };

        return response.redirect("/feedback");
      }
      return response.send("Feedback form posted");
    }
  );
  return router;

Why aren't the form values passing to the router's post method?

bunkley
  • 19
  • 5
Sartorialist
  • 191
  • 14

1 Answers1

0

You need to access form fields in request.body followed by their respective name as described in THIS post.

Furqan Aziz
  • 1,094
  • 9
  • 18