Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
express-validator error message attributes and location

I am using expressJS, Node and Handlebars.

In my post function I have the following:

routes.js

req.assert("referenceNumber", "Enter ...").notEmpty();
req.assert("yourPostcode", "Enter ...").notEmpty();

var errors = req.validationErrors(true);

if (errors) {
   res.render('<page>/index', {
   title: "<title>",
   errors: errors
});

} else {
   // Redirection
}

index.hbs

{{#if errors}}
 <ul>
  {{#each errors}}
    <li><a href="#">{{this.msg}}</a></li>
  {{/each}}
 </ul>
{{/if}}

This outputs a list of errors on submission which is great. The problem is I need the error message located next to the form field as well as.

So ideally I can target each error by name e.g. "referenceNumber", "yourPostcode".

I have thought about maybe having a variable inside the routes.js like so:

if (errors) {
   res.render('<page>/index', {
   title: "<title>",
   referenceNumber: <something>,
   yourPostcode: <something>
});

And perhaps be able to do something funky this way.

It would also be very good associating an id with each error message to act as an anchor link to each error form field.

Totally stumped with this one, and would really appreciate some help.

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda