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!