2

im re-rendering my form with any errors,

// controller.js 

(req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      res.render("quotes", {
        form: req.body,
        errors: errors.array()
      });
      return;
    } 

and im able to display the errors in a list like this

// ejs file

 <% if (locals.errors) { %>

       <ul>

          <% errors.forEach(error => { %>
          <li><%= error.msg %></li>
          <% }) %>

        </ul>
  <% } %>



but id like to access each error individually, so i can display them under the invalid input instead of all of them in a list at the bottom of the form.

below is my closest solution, it certainly isn't correct but its as close as ive gotten. (it breaks depending on what other errors are or aren't present)

//controller.js

(req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      res.render("quotes", {
        form: req.body,
        errors: errors.array()
        error1: errors.array()[0].msg,
        error2: errors.array()[1].msg,
        error3: errors.array()[2].msg,
      });
      return;
    } 


// ejs file

<% if (locals.errors) { %> <li> <%= error1 %></li> <% } %>


errors: [
    {
      value: '',
      msg: 'First name must be specified',
      param: 'firstName',
      location: 'body'
    },
    {
      value: '',
      msg: 'Last name must be specified',
      param: 'lastName',
      location: 'body'
    },
    {
      value: '',
      msg: 'Please enter a valid email',
      param: 'email',
      location: 'body'
    }
  ] 

any help would be appreciated!

2 Answers 2

1

any help would be appreciated!

Ok, here is my suggestion.

You need to understand which error goes to which input field. As an example (if you can do it) you can change the errors object (array) to something like this:

let errors = {
    'firstName': {
      value: '',
      msg: 'First name must be specified',
      location: 'body'
    },
    'lastName': {
      value: '',
      msg: 'Last name must be specified',
      location: 'body'
    },
    'email': {
      value: '',
      msg: 'Please enter a valid email',
      location: 'body'
    }
};

So now your errors has keys (if no errors, then let errors = {}).

And so you can add an error message to any input field:

<% if (locals.errors && errors.firstName) { %> <div class="error"><%= errors.firstName.msg %></div> <% } %>
<input type="text" id="firstName" name="firstName"/>

<% if (locals.errors && errors.lastName) { %> <div class="error"><%= errors.lastName.msg %></div> <% } %>
<input type="text" id="lastName" name="lastName"/>

If you need to convert your errors array into the object, you can do it:

let errorsObj = {};
errors.map((item) => {
  const id = item.param;
  delete item.param;
  errorsObj[id] = item;
});

Run the code above in the place where you have your errors array, but before rendering to the page. So, the page will be rendered as I wrote, but change errors to errorsObj:

<% if (locals.errors && errorsObj.firstName) { %> <div class="error"><%= errorsObj.firstName.msg %></div> <% } %>
<input type="text" id="firstName" name="firstName"/>
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the suggestion, unfortunately i dont think i can change the array (or i dont know how) since its being created through the 'validationResult'
@jroc You can convert on the front-end. I've updated my answer.
this is perfect! thank u so much, i really appreciate it!!
0

quick fix :

//controller.js

(req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      res.render("quotes", {
        form: req.body,
        errors: errors.array()
        error1: errors.array()[0] && errors.array()[0].msg,
        error2: errors.array()[1] && errors.array()[1].msg,
        error3: errors.array()[2] && errors.array()[2].msg,
      });
      return;
    } 

Also checkout lodash

1 Comment

unfortunately this solution doesn't seem to work, if the 1st error isn't true, then the 2nd error becomes the 1st, which makes it impossible to display in the correct spot. thanks for the reply though and for recommending lodash, i will definitely check that out. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.