为什么模板中未显示来自validate()方法的错误消息? [英] Why error message from validate() method is not displayed in the template?
问题描述
Reason for Re-posting: I have already posted this question HERE
, but I haven't got any answer but a comment which wasn't clear. So I am thinking to post it back again for some help.
SO
我有一种验证方法来检查两个密码(密码和确认密码)是否匹配.
I have a got a validation method for checking if both passwords (Password & Confirm password) have been matched or not.
这是我的验证方法:
public String validate() {
if (!this.password.equals(this.passwordConfirm)) {
return "Passwords do not match";
}
return null;
}
在我的scala中,我有以下内容.
And in my scala I have got as following.
@if(accountForm.hasGlobalErrors) {
<p class="error">
@accountForm.globalError.message
</p>
}
@if(flash.contains("success")) {
<p class="success">
@flash.get("success")
</p>
}
@helper.inputPassword(accountForm("password"),'_label -> "Password", 'placeholder -> "**********")
@helper.inputPassword(accountForm("passwordConfirm"),'_label -> "Confirm Password", 'placeholder -> "**********")
问题:但是,在字段中输入了两个不同的密码后,一切都按预期工作,但未显示从validate()
方法返回的错误消息.我不知道为什么.以下是我的CSS错误.
QUESTION : Everything is working as expected BUT when two different passwords have been entered in the fields, it is NOT displaying the error message returned from validate()
method. I have no idea why. Following is my CSS for error.
p.error{
border-style: outset;
border-radius: 5px;
color: red;
font-weight: bold;
background-color: white;
padding: 10px 10px 10px 10px;
}
更新:我从评论中得知,我已经丢失了一些动作,但是我不太明白.我是新手,非常感谢您提供任何明确的帮助.
UPDATE : I got to know from a comment that, I HAVE BEEN MISSING some action but I didn't quite get it. I am new to this stuff, any clear help is much appreciated.
更新:我尝试按以下方式在validate()
方法中编写return语句:
UPDATE : I have tried writing the return statement in validate()
method as below:
return Context.current().flash().put("error", "Passwords do not match");
这都不起作用!问题是,它显示了return语句,但是在保存了本不应该发生的页面之后!
This neither worked! The problem with this is that, it is displaying the return statement but after saving the page which shouldn't be happening!
推荐答案
我无法发表评论(必须拥有50个声誉),因此我将在此处提出问题.绑定请求中的表单的控制器操作的内容是什么?
I can't comment (must have 50 reputation to do), so I'll ask the question here. What's the content of your controller action that binds the form from the request?
您必须具有以下内容:
public final Result submit() {
Result result = null;
Form<Something> boundForm = Form.form(Something.class).bindFromRequest();
if (boundForm.hasErrors()) {
result = badRequest(sometemplate.render(boundForm));
} else {
potentiallyDoSomethingWith(boundForm.get());
result = redirect(route.Application.somewhere());
}
return result;
}
不是吗?
这篇关于为什么模板中未显示来自validate()方法的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!