Friday 17 March 2017

Flash message multiple errors in list format

I am trying to show flash messages as errors but multiple errors are shown in 1 flash. How can I seperate them or at least show them vertically in a list format?

passport.use('local-signup', new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
      function(req, email, password, done) {

        var name = req.body.name;
        var email = req.body.email;
        var password = req.body.password;
        var password2 = req.body.password2;

        // validation
        req.checkBody('name', 'Name is required').notEmpty();
        req.checkBody('email', 'Email is required').notEmpty();
        req.checkBody('email', 'Email is not valid').isEmail();
        req.checkBody('password', 'Password is required').notEmpty();
        req.checkBody('password', 'Password must be at least 8 characters').len(8, 20);
        req.checkBody('password2', 'Passwords do not match').equals(req.body.password);


        var errors = req.validationErrors();
        console.log(errors);
        if(errors) {
          for (each in errors) {
            req.flash('signupMessage', errors[each].msg)
          }
          return done(null, false);
        }


        User.findOne({ 'local.email' :  email }, function(err, user) {

            if (err)
                return done(err);
            if (user) {
                return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
            } else {

                var newUser            = new User();
                newUser.local.email    = email;
                newUser.local.password = newUser.generateHash(password);

                newUser.save(function(err) {
                    if (err)
                        throw err;
                    return done(null, newUser);
                });
            }
        });
      }));

The errors are now shown in 1 flash message. How would I show them seperate, or at least vertically?

if(errors) {
              for (each in errors) {
                req.flash('signupMessage', errors[each].msg)
              }
              return done(null, false);
            }

enter image description here



via Soundwave

No comments:

Post a Comment