Ank*_*kit 4 node.js express handlebars.js
发生错误时如何保留表单数据。通常当表单提交页面重新加载时。我正在使用把手、快速验证器、连接闪存。这是我的注册代码:
//Register User
router.post('/register', function(req, res){
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
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('username', 'Username is required!').notEmpty();
req.checkBody('password', 'Password is required!').notEmpty();
req.checkBody('password', 'Minimum Length of Password is 1!').isLength({min: 1});
req.checkBody('password2', 'Password do not match!').equals(req.body.password);
var errors = req.validationErrors();
if(errors){
//want some help here by which if any error generate then all value is automatically filled. So that everytime there is no need to fill full form
res.render('register', {
errors: errors
});
}else{
//Here Success Part Code runs
}
});
Run Code Online (Sandbox Code Playgroud)
这段代码是 view part(register.handlebars) //任何帮助将不胜感激。 这个页面是车把。关于显示旧数据的形式,如果表单生成错误Littlebit搞不清 注册
{{#if errors}}
{{#each errors}}
<div class="alert alert-danger">{{msg}}</div>
{{/each}}
{{/if}}
<form method="post" action="/custom/register">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<label>Username</label>
<input type="text" name="username" class="form-control" placeholder="Userame">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<label>Confirm-Password</label>
<input type="password" name="password2" class="form-control" placeholder="Password">
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
//Register User
router.post('/register', function(req, res){
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
//fields value holder
var form = {
nameholder: req.body.name,
usernameholder: req.body.username,
emailholder: req.body.email
};
//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('username', 'Username is required!').notEmpty();
req.checkBody('password', 'Password is required!').notEmpty();
req.checkBody('password', 'Minimum Length of Password is 1!').isLength({min: 1});
req.checkBody('password2', 'Password do not match!').equals(req.body.password);
var errors = req.validationErrors();
if(errors){
res.render('register', {
errors: errors,
form:form
});
}else{
//Here Success Part Code runs
}
});
//register.handlebars page
//just add value with holder
<h2 class="page-header">Register</h2>
{{#if errors}}
{{#each errors}}
<div class="alert alert-danger">{{msg}}</div>
{{/each}}
{{/if}}
<form method="post" action="/custom/register">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" placeholder="Name" value="{{form.nameholder}}">
</div>
<div class="form-group">
<label>Username</label>
<input type="text" name="username" class="form-control" placeholder="Userame" value="{{form.usernameholder}}">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control" placeholder="Email" value="{{form.emailholder}}">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" placeholder="Password" >
</div>
<div class="form-group">
<label>Confirm-Password</label>
<input type="password" name="password2" class="form-control" placeholder="Confirm-Password">
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2980 次 |
| 最近记录: |