Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda