0

I have two seperate Models AccountDetailsForm and AccountDetails, former for form submission and latter for persistence.

AccountDetailsForm:

public class AccountDetailsForm {

    private AccountDetails accountDetails;

    @NotNull
    private String confirmPassword;

    //setter and getters
}

and AccountDetails:

@Entity
@Table(name = "ACCOUNT")
public class AccountDetails {

    @Id
    @Column(name = "USER_NAME")
    @NotEmpty
    @Max(value = 60)
    private String userName;


    @Column(name = "PASSWORD")
    @NotEmpty
    @Min(value = 8)
    private String password;

   //setters and getters
}

Now I am validating the submitted form using @Valid:

@RequestMapping(value="userRegistration", method = RequestMethod.POST)
public ModelAndView registerUser(@Valid AccountDetailsForm accountDetailsForm, BindingResult result,Model model){

    //I autowire localValidatorFactoryBean to validate the fields in AccountDetails bean.
    localValidatorFactoryBean.validate(accountDetailsForm.getAccountDetails(), result);

    if (result.hasErrors()){
        model.addAttribute("accountDetailsForm", new AccountDetailsForm());
        model.addAllAttributes(result.getModel());
        return new ModelAndView("userRegistration");
    }

I am using hibernate for persistence. Is there a way to validate the accountDetails bean using @Valid rather than explicitly calling validate() method or using spring validator implementation?

I don't mind hearing suggestions for a confirmPassword workaround. I am following the current approach because it doesn't make sense to have a confirmPassoword field in the model.

jelies
  • 9,110
  • 5
  • 50
  • 65
shazinltc
  • 3,616
  • 7
  • 34
  • 49

2 Answers2

1

Put @Valid in AccountDetails object of AccountDetailsForm:

public class AccountDetailsForm {

    @Valid
    private AccountDetails accountDetails;

    @NotNull
    private String confirmPassword;

        //setter and getters
}

When AccountDetailsForm is validated with @Valid in your controller, AccountDetails will be validated too.

Reference example here.

jelies
  • 9,110
  • 5
  • 50
  • 65
  • Thanks for the answer, but that doesn't work. There are still no errors in BindingResult object! – shazinltc Aug 23 '12 at 08:27
  • Just added an example from the hibernate validator reference, check it out. Are you using these imports? `javax.validation.constraints.NotNull` – jelies Aug 23 '12 at 08:43
  • Do you have the hibernate validator jar in your classpath? – Varun Achar Aug 23 '12 at 08:45
  • No, i am using hibernate validation @NotEmpty. – shazinltc Aug 23 '12 at 08:45
  • Check this question http://stackoverflow.com/questions/7545231/notnull-notblank-and-notempty-bean-validation-does-not-work-in-jsf – Varun Achar Aug 23 '12 at 08:49
  • It's ok to use `org.hibernate.validator.constraints.NotEmpty` (better than `@NotNull` in Strings). Check @VarunAchar 's link, may help you. – jelies Aug 23 '12 at 08:49
  • Yea i have both hibernarte validator and validation-api (javax.validation). – shazinltc Aug 23 '12 at 09:01
  • hibernate validator 4.2 and validation api 1.0.0. – shazinltc Aug 23 '12 at 09:01
  • what application server are you running your application? – jelies Aug 23 '12 at 09:54
  • I am running it on tomcat. I tried using @valid on a normal bean (No object graph), it works fine. The isssue is only with object graph validation. – shazinltc Aug 23 '12 at 10:10
  • Maybe this is not causing the problem, but looking to your `AccountDetails` class, remember that for `String` size validation you have to use @Size(min=X, max=Y). `@Max` and `@Min` are used to validate numbers (see [reference](http://docs.oracle.com/javaee/6/api/javax/validation/constraints/Min.html)). Perhaps for this, the validation over userName & password of `AccountDetails` is not firing. – jelies Aug 23 '12 at 10:59
  • I tried with NotBlank, but doesn't show errors, instead throws exception on persisting – shazinltc Aug 23 '12 at 11:20
  • I have finally found out at the reason :D I was using an instance of spring 3 validator, as I had to do some other validations too :) When I removed that it works fine. Thanks. – shazinltc Apr 07 '13 at 13:10
0

Look at the groups field inside the annotations: you could specify the dependant classes to apply. Here it could be something like that:

@Valid(groups=AccountDetails.class)

With maven, i currently use these versions of the libs on a Spring MVC 3.1 project:

springframework 3.1.2.RELEASE
javax.validation 1.0.0.GA
hibernate-validator-annotation-processor 4.1.0.Final
BendaThierry.com
  • 2,080
  • 1
  • 15
  • 17
  • It is true, didn't realize... Anyway you could add `@NotEmpty(groups=...)` and `@Size(groups=...)` to your object graph `Account` && `AccountDetails` fields. Maybe look at your DAO's too: the rules must match. – BendaThierry.com Aug 23 '12 at 14:07
  • 1
    `@Valid` doesn't take groups, but since 3.1 there is Spring annotation `@Validated({...})` that works in the same way and takes groups ([SPR-6373](https://jira.springsource.org/browse/SPR-6373)) – Maciej Majewski Sep 04 '13 at 12:05