2013-11-23 60 views
0

我正在通過POST方法驗證表單。當電子郵件地址已經註冊時,我想將錯誤推送到express-validator,所以當我執行req.validationErrors()時,它將包含在內。類似這樣的:如何使用express-validator拋出自定義錯誤?

req.checkBody('inputFirstName', 'First name empty').notEmpty(); 
req.checkBody('inputLastName', 'Last name empty').notEmpty(); 
db.User.find({email: req.body.inputEmail}, function (err, user){ 
     if(user){ 
      var error = {param: "inputEmail", msg: "Email address already registered", value: ''}; 
      expressValidator._errors.push(error); 
     } 
    }); 

謝謝。

回答

4

你可以做這樣的事情:

req.checkBody('inputFirstName', 'First name empty').notEmpty(); 
req.checkBody('inputLastName', 'Last name empty').notEmpty(); 
var errors = req.validationErrors(); 
db.User.find({email: req.body.inputEmail}, function (err, user){ 
     if(user){ 
      var error = {param: "inputEmail", msg: "Email address already registered", value: req.body.email}; 
      if (!errors) { 
       errors = []; 
      } 
      errors.push(error); 
     } 

     // Here need to be next part of action - not outside find callback! 
    }); 

但我認爲這是一個壞主意,把在請求端驗證一些貓鼬驗證。您可以使用一些模式方法,例如預先保存,在那裏進行驗證。

0

以下:

在app.js:

... 
var session = require('express-session'); 
var passport = require('passport'); 
var flash = require('connect-flash'); 
var validator = require('express-validator'); 
... 
var app = express(); 
require('./config/passport'); 
... 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(validator()); 
... 
app.use(session({ secret: settings.session_key, resave: false, saveUninitialized: false })); 
app.use(passport.initialize()); 
app.use(passport.session()); 
app.use(flash()); 

在路由器/ index.js:

router.get('/register', function (req, res, next) { 
    var messages = req.flash('error'); 
    res.render('customers/register', { 
     messages: messages, 
     hasErrors: messages.length > 0 
    } 
} 

router.post('/register', passport.authenticate('local.register', { 
    successRedirect: '/customer/dasdboard', 
    failureRedirect: '/customer/register', 
    failureFlash: true //required 
})); 

在查看/客戶/ signup.hbs:

{{#if hasErrors}} 
    <div class="alert alert-danger"> 
     {{#each messages}} 
       <p>{{this}}</p> 
     {{/each}} 
    </div> 
{{/if}} 

在config/passport.js中:

passport.use('local.register', new LocalStrategy({ 
    usernameField: 'email', 
    passwordField: 'password', 
    passReqToCallback: true //required 
}, function(req, email, password, done) { 

    req.checkBody('inputFirstName', 'First name empty').notEmpty(); 
    req.checkBody('inputLastName', 'Last name empty').notEmpty(); 
    req.checkBody('inputEmail', 'Invalid email').notEmpty().isEmail(); 
    req.checkBody('password','Confirm password does not match.').equals(req.body.confirmpassword); 

    var errors = req.validationErrors(); 
    if (errors) { 
     var messages = []; 
     errors.forEach(function(error){ 
      messages.push(error.msg); 
     }); 
     return done(null, false, req.flash('error', messages)); 
    } 

    db.User.find({email: req.body.inputEmail}, function (err, user) { 
     if (err) { 
      return done(err); 
     } 
     if (user) { 
      return done(null, false, {message: 'Email address already registered!'}); 
     } 

     //Save new data here 
     .... 
    }); 
})); 

爲我的解決方案工作。

相關問題