Andres Vera
Andres Vera

Reputation: 72

save the values of the input in case of error in node.js

when there is a validation error the page is reloaded and rendered with the error message, but all fields are cleaned, how can I do so that when there is an error the fields are not cleaned?

app.post('/email', (req, res, next)=>{

req.checkBody("email", "ingrese un correo valido").isEmail();
req.checkBody("name", "solo letras en el campo nombre").isAlpha();

var errors = req.validationErrors();

if(errors){
    res.render('index', {errors: errors});
    return;
}else{
       // normal processing
}

});

Upvotes: 0

Views: 30

Answers (1)

user1092803
user1092803

Reputation: 3297

You should pass to the template also the inputs, so the line should be something likeres.render('index', {errors: errors, inputs: inputs}); and then in the template render the page according to which variables you have

Upvotes: 1

Related Questions