我正在使用 hibernate 注释通过 spring 表单验证来验证该字段。
这是我的模型类:
public class NewPasword {
@NotEmpty(message="Please Provide Password")
@Pattern(regexp = "^(?=.*?[A-Z])(?=.*?[0-9])(?=\\S+$).{8,}$", message = "{encryptedpassword.required}")
@Size(min=8, max=19)
private String newPassword;
........
.......
}
这是我的 JSP;
.....
<div class="row">
<form:input path="field" />
<form:error path="field" />
</div>
.....
在执行所有验证消息时,前端不应该显示消息。
任何人都可以建议我如何才能从上往下仅获得一条验证消息。
如:如果字段为空,则消息应仅为“请输入字段”
如果字段与模式不匹配,则消息应仅为“请输入有效字段”
如果字段为 <8 且 >19,则消息应仅为“长度应在 8-19 之间”
如果需要更多信息,请询问。
最佳答案
您可以使用Group
和GroupSequence
来解决这个问题
@GroupSequence({ Step1.class, Step2.class, Step3.class })
public class NewPasword {
public interface Step1 {}
public interface Step2 {}
public interface Step3 {}
@NotEmpty(message="Please Provide Password",groups = Step1.class)
@Pattern(regexp = "^(?=.*?[A-Z])(?=.*?[0-9])(?=\\S+$).{8,}$", message = "{encryptedpassword.required}", groups = Step2.class)
@Size(min=8, max=19, groups = Step3.class)
private String newPassword;
}
参见引用:Grouping
关于java - 所有验证消息都可以在带注释驱动的 hibernate 验证中看到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29966596/