【发布时间】:2014-03-30 04:04:15
【问题描述】:
我在玩 Play 时遇到了很大的困难! 2.2.x 文档,我目前被困在如何从我的表单验证中显示错误。
这是我的代码:
路线
GET /account/create controllers.Account.create()
POST /account/create controllers.Account.createAccount()
型号
public static UserAccount create(UserAccount data){
UserAccount account = data;
String salt = BCrypt.gensalt();
account.email = data.email;
account.salt = salt;
account.hash = BCrypt.hashpw(data.hash, salt);
account.save();
return account;
}
控制器
// handles POST method
public static Result createAccount(){
Form<UserAccount> userForm = form(UserAccount.class).bindFromRequest();
if(userForm.hasErrors()){
return badRequest();
}else{
UserAccount.create(userForm.get());
Logger.info("Username is: " + userForm.get().email);
return ok("ok, I recived POST data. That's all...");
}
}
// Handles GET method
public static Result create(){
return ok(
views.html.account.form.render()
);
}
观看次数
@if(form.hasGlobalErrors) {
<p class="error">
@form.globalError.message
</p>
}
@helper.form(action = routes.Account.createAccount()) {
<input type="text" name="email" placeholder="Your Email Address"/><br/>
<input type="password" name="password" placeholder="Your Password"/><br/>
<input type="text" name="fname" placeholder="Your First Name"/><br/>
<input type="text" name="midname" placeholder="Your Middle Name"/><br/>
<input type="text" name="lname" placeholder="Your Last Name"/><br/>
<input type="text" name="dob" placeholder="Your Birthday"/><br/>
<select name="gender" id="gender">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Undecided</option>
</select><br/>
<input type="submit" value="Login" />
}
错误信息
value hasGlobalErrors is not a member of object views.html.account.form
谁能告诉我我的代码有什么问题?我对给定的例子感到沮丧。
编辑#1:
这是我到目前为止所做的:
型号:
public static UserAccount create(UserAccount data){
UserAccount account = data;
String salt = BCrypt.gensalt();
account.email = data.email;
account.salt = salt;
account.hash = BCrypt.hashpw(data.hash, salt);
account.save();
return account;
}
控制器:
// HANDLES GET REQUEST
public static Result create(){
return ok(
views.html.account.form.render(userForm)
);
}
// HANDLES POST REQUEST
public static Result createAccount(){
Form<UserAccount> userForm = form(UserAccount.class).bindFromRequest();
if(userForm.hasErrors()){
return badRequest(views.html.account.form.render(userForm));
}else{
// UserAccount.create(userForm.get());
// Logger.info("Username is: " + userForm.get().email);
UserAccount data = userForm.get();
return ok(data.email);
}
}
视图/模板
@(form: Form[UserAccount])
@if(form.hasGlobalErrors) {
<h1>Please fix the following error first</h1>
<p>
@form.globalError.message
</p>
<ul>
@for(error <- form.globalErrors) {
<li>@error.message</li>
}
</ul>
}
@helper.form(action = routes.Account.createAccount()) {
<input type="text" name="email" placeholder="Your Email Address"/><br/>
<input type="password" name="password" placeholder="Your Password"/><br/>
<input type="text" name="fname" placeholder="Your First Name"/><br/>
<input type="text" name="midname" placeholder="Your Middle Name"/><br/>
<input type="text" name="lname" placeholder="Your Last Name"/><br/>
<input type="text" name="dob" placeholder="Your Birthday"/><br/>
<select name="gender" id="gender">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Undecided</option>
</select><br/>
<input type="submit" value="Login" />
}
到目前为止,根据萤火虫,当我故意将错误放在表单上时,服务器将返回 badrequest。但是,模板不会显示错误。
如果我这样更改控制器:
public static Result createAccount(){
Form<UserAccount> userForm = form(UserAccount.class).bindFromRequest();
if(userForm.hasErrors()){
return ok(userForm.errorsAsJson().toString());
}else{
// UserAccount.create(userForm.get());
// Logger.info("Username is: " + userForm.get().email);
UserAccount data = userForm.get();
return ok("ok, I received POST data. That's all...");
}
}
或者如果我在我的视图/模板上这样做
<pre>@form.errorsAsJson.toString()</pre>
它可以工作,并且会相应地打印错误。有人知道我在这里缺少什么吗?
编辑#2:
最适合我输出错误的方法是在我的视图/模板上执行此操作
@(form: Form[UserAccount])
@if(form.hasErrors) {
<h1>Please fix the following error first</h1>
<ul>
@for(error <- form.errors) {
<li>@error.toString</li>
}
</ul>
}
哪个输出这个:
(email,[ValidationError(email,error.required,[])])
(hash,[ValidationError(hash,error.required,[])])
由于我试图向用户显示适当的消息,因此该消息毫无用处。
【问题讨论】: