2014-08-28 44 views
0

我的目标很简单。如果用户正在尝试注册并在所有必填字段已满之前点击“提交”,我想将他或她重新导向到注册页面,但稍微更改一下,以便在每个必填字段旁边显示一个红色星号(这是我知道)任意和丑陋的选择。我想我不必为此目的单独看待。是否有可能以这种方式改变注册视图?如何将用户重定向到注册页面,但在必填字段旁边带有红色星号?

在routes.js

// process the signup form 
    app.post('/signup', passport.authenticate('local-signup', { 
     successRedirect : '/profile', // redirect to the secure profile section 
     failureRedirect : '/signup?missingFields=true', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 

在signup.ejs

% if (req.params.missingFields) {%> 
     BLAH 
    <% } %> 

我得到signup.ejs一个错误 “REQ没有在EVAL规定” 以下是完整的错误文本:

ReferenceError: /home/adam/Documents/Node/node-authentication/views/signup.ejs:26 24| --> 25| >> 26| <% if (req.params.missingFields) {%> 27| BLAH 28| <% } %> 29| req is not defined at eval (eval at (/home/adam/Documents/Node/node-authentication/node_modules/ejs/lib/ejs.js:237:14), :31:825) at eval (eval at (/home/adam/Documents/Node/node-authentication/node_modules/ejs/lib/ejs.js:237:14), :31:2194) at /home/adam/Documents/Node/node-authentication/node_modules/ejs/lib/ejs.js:250:15 at Object.exports.render (/home/adam/Documents/Node/node-authentication/node_modules/ejs/lib/ejs.js:288:13) at View.exports.renderFile [as engine] (/home/adam/Documents/Node/node-authentication/node_modules/ejs/lib/ejs.js:318:20) at View.render (/home/adam/Documents/Node/node-authentication/node_modules/express/lib/view.js:76:8) at Function.app.render (/home/adam/Documents/Node/node-authentication/node_modules/express/lib/application.js:502:10) at ServerResponse.res.render (/home/adam/Documents/Node/node-authentication/node_modules/express/lib/response.js:777:7) at Object.handle (/home/adam/Documents/Node/node-authentication/routes/routes.js:35:7) at next_layer (/home/adam/Documents/Node/node-authentication/node_modules/express/lib/router/route.js:103:13)

+0

为什么包含'mongodb'和'passport'标签? – 2014-08-28 03:07:24

+0

我使用这些,我认为可能有一些包,或者我不知道这可以帮助我。 – michaelAdam 2014-08-28 03:09:33

回答

1

首先,在发布或将用户发送到其他地方之前,您应该检查数据。一旦你认为数据是正确的,并通过了所有验证检查,你可以发布它。

您可以使用简单的ajax来检查生动的字段并将星号放在字段旁边。

但是,如果由于某种原因,你不能这样做,你可以重定向使用

res.redirect('/path')

用户,并把星号使用

res.redirect('/path?errorInField1=true')

您可以用比得到该数据与req.params.errorInField1并采取相应的行动

+0

我明白如何做到这一点,我问我如何将他们重定向回注册页面,但改变它的外观。我现在意识到,我应该在客户端JS做这件事。 – michaelAdam 2014-08-28 03:04:39

+0

将该部分添加到答案:) – 2014-08-28 03:06:19

+0

那么我是否需要使用decodeURIComponent来检查浏览器中的GET变量? – michaelAdam 2014-08-28 03:11:15

0

做验证客户端。

在我看来,通过ajax提交请求会更好,如果您提交表单,则必须在发生错误时重新导向回去。

1

为页面的get方法创建一个post方法。首先安装body parser来获取帖子值。

$ npm install --save body-parser 

然后初始化它。

var bodyParser = require('body-parser') 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded()); 

app.use(express.json()); 
app.use(express.urlencoded()); 

现在验证用户输入。

router.post('/register' function(req, res){ 
    var email = req.body.email; 
    var name = req.body.name; 
    ..... // Other required fields. 
    if(email || name ...//Other variables to validate for null){ 
     req.flash('error', 'All the fields are required completing'); 
     req.redirect() 
    } 
}); 
相关问题