Results 1 to 3 of 3

Thread: How do I associate a class-level validator with a field?

  1. #1

    Default How do I associate a class-level validator with a field?

    Hi all,

    I have a custom class-level JSR-303 validator to confirm that the value in the 'confirmPassword' field matches the value in the 'password' field. Does anyone know how to associate the error message from this validator with the confirmPassword field?

  2. #2

    Default

    I think, that should be trivial (in my case I check that emails are the same):

    Code:
    final Object email = errors.getFieldValue("email");
    final Object emailConfirmation = errors.getFieldValue("emailConfirmation");
    
    if (!ObjectUtils.equals(email, emailConfirmation)) {
    	errors.rejectValue("emailConfirmation", "required", null, "Emails should match");
    }
    In JSP:

    Code:
    E-Mail Address: <form:errors path="email" cssClass="error" /><form:input path="email" size="25" />
    Re-enter E-Mail Address: <form:errors path="emailConfirmation" cssClass="error" /><form:input path="emailConfirmation" size="25" />

  3. #3

    Default

    Thanks for the suggestion but I was looking for a way to do it with a custom class-level JSR-303 validator. I think your suggestion is using Spring MVC validation.

    I had a closer look at the Hibernate Validator documentation and I found in there how to set a custom property path for a class-level validator.

    http://docs.jboss.org/hibernate/stab...nstraints.html
    Last edited by kevinstembridge; Jul 8th, 2011 at 04:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •