Spring MVC 3 验证注释 - 确认电子邮件
我正在尝试使用 Spring 以及集成 JSR-303 验证和 Hibernate 实现来验证表单。我有一个“确认电子邮件”(emailConf
) 字段,我想确认它是否等于 email
字段。我看到有人这样做的帖子:
public class ContactInfoForm {
@NotEmpty
private String name;
@NotEmpty
@Email
private String email;
@Expression(value = "emailConf= email", applyIf = "emailConf is not blank")
private String emailConf;
...
}
然而,这个 emailConf
验证不起作用(即当字段不匹配时不会发生错误)。我看过一些教程,它们显示了类似的注释,但无法再找到它们或任何有关其工作原理的文档。有谁知道通过注释验证“确认电子邮件/密码”字段的方法?我目前正在使用替代方案,即实现 Validator 并验证 validate 方法中的确认字段。
I am trying to validate a form using Spring with integrated JSR-303 validations with Hibernate implementation. I have a "confirm email" (emailConf
) field that I would like to confirm is equal to the email
field. I saw a post of someone doing it like this:
public class ContactInfoForm {
@NotEmpty
private String name;
@NotEmpty
@Email
private String email;
@Expression(value = "emailConf= email", applyIf = "emailConf is not blank")
private String emailConf;
...
}
However, this emailConf
validation is not working (i.e. no error occurs when the fields don't match). I've seen a couple tutorials that have shown similar annotations, but can't find them anymore or any documentation on how this works. Does anyone know a way to validate "confirm email/password" fields through annotation? I am currently using the alternative, which is to implement Validator
and validate the confirm fields in the validate method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该看看这个问题及其答案:有很多方法讨论了如何进行这样的验证(它关于密码和密码确认,但问题是相同的)。
May you should have look at this question and its answers: there are many ways discussed how to do a such a validation (it is about password and password confirm, but the problem is the same).
您需要的是 “类级约束”< /a> (如 JSR-303 所描述)如果您想比较同一类的 2 个字段。我认为您的
@Expression
不会以这种方式工作。What you need is a "Class-level constraint" (as described by JSR-303) if you want to compare 2 field of the same class. I don't think your
@Expression
will work that way.