S
S
SpideR-KOSS2018-09-04 14:43:54
MongoDB
SpideR-KOSS, 2018-09-04 14:43:54

How to render NodeJS form input?

Good afternoon!
There is a registration form. It is validated using the Express-Validator. When submitting the form, it is checked and errors are displayed, while the form is reset, and I need to save the data in those fields where there are no errors.
How to do it?

check('name').isLength({ min: 5 }).withMessage('Имя должно составлять не менее 5 символов!'),
      check('email').isEmail().withMessage('Проверьте правильность E-mail!'),
      check('password', ).isLength({ min: 5 }).withMessage('Пароль должен содержать не менее 5 символов!'),
      check('passwordConfirm', 'Пароли не совпадают!')
        .custom((val, { req }) => {
            return val === req.body.password;
          }),
      ], (req, res) => {
        const name = req.body.name;
        const email = req.body.email;
        const password = req.body.password;
        const passwordConfirm = req.body.passwordConfirm;

        // Константа с ошибками
        const errors = validationResult(req);
    
        // Проверка на ошибки
        if (!errors.isEmpty()) {
          // Render template with errors
          return res.render('registration', {
            
            errors: errors.array(),
          })
        } else {
          // Render template without errors
          return res.render('index', {
            success: 'Success'
          })
        }});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question