Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js (Express) Form Clears on Submission
    primarykey
    data
    text
    <p>I am working on a really basic registration form in Node.js (with Express), and I am trying to find the easiest way to provide basic form validation. I've gone with "Express-Validator", which seems to do a fine job. However, my goal is to simply show any validation messages that are required and to <strong>leave the values entered by the user alone.</strong></p> <p>It seems that the request information is not making it back into the res.render, which I guess makes sense. However, I've looked everywhere I can think of and I can't find any reference that discusses how to keep form fields populated after showing error messages.</p> <p>Below is a small snippet describing my approach:</p> <pre><code>post: function(req, res){ var userName = req.body.username; var password = req.body.password; //Validate input req.assert("username", 'Invalid email address.').isEmail(); req.assert("password", 'Password cannot be empty.').notEmpty(); req.assert("passwordConfirm", 'Passwords entered do not match!').equals(password); //Make sure we have no validation errors var pageErrors = req.validationErrors(); if(!pageErrors) { userModel.CreateUser(userName, password, function(err){ if(err) { //there was a problem inserting new user... probably already exists //will need to check the error to confirm var dbErrorMessage = "Could not insert record into database!"; if(err.code === 11000) { //this is a duplicate entry dbErrorMessage = "A user with that email address already exists!"; } res.render('register.html', { pageErrors: [{msg: dbErrorMessage }]}); } else { res.render('register.html', { successMessage: successMessage }); } }); } else { res.render('register.html', { pageErrors: pageErrors }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload