Spring MVC 3 验证注释 - 确认电子邮件

发布于 2024-12-10 12:36:01 字数 589 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰ゝ天使的微笑 2024-12-17 12:36:01

也许您应该看看这个问题及其答案:有很多方法讨论了如何进行这样的验证(它关于密码和密码确认,但问题是相同的)。

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).

永言不败 2024-12-17 12:36:01

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文