3

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.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
stephen.hanson
  • 9,014
  • 2
  • 47
  • 53
  • Where is this `@Expression` annotation from? – Ralph Oct 17 '11 at 16:28
  • I didn't realize it, but the @Expression annotation is from "spring-modules" which I didn't even realize was still in my classpath. I had put this in my classpath when I was originally following a validation tutorial, but ended up using JSR-303 and Hibernate Validator. I'm not very familiar with spring-modules, but the forum page on SpringSource has an announcement from Aug 2010 saying "Spring Modules forum decommissioned in favor of Spring Extensions". If it's no longer supported, I would prefer not to use this library. – stephen.hanson Oct 17 '11 at 22:46

2 Answers2

1

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

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • 1
    I'm using an implementation based on that FieldMatch validator and it works great. – digitaljoel Oct 17 '11 at 19:01
  • I took a look at the page, and there are some really good resources there. One thing I can't figure out is how to associate the result of a class-level constraint to a specific field of my form. If the emails don't match, I would like my jsp to output the error as an error of the emailConf field. I am currently outputting errors using springs form:errors tag: `` – stephen.hanson Oct 17 '11 at 22:51
  • Thanks for the resource. The custom FieldMatch annotation seems like a good way to do it. I also was able to get scriptassert working for my needs, but my code was a little messy getting the error to bind to the appropriate form field. In the end, I just ditched annotations and went with a custom validator class. If I had more time I would have experimented with these other solutions. Thanks! – stephen.hanson Oct 21 '11 at 20:55
1

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.

jeha
  • 10,562
  • 5
  • 50
  • 69