Baakan
Baakan

Reputation: 139

Spring, JSR 303 - Validation groups on nested fields

Here is the example, better than words :

public class Person{

   private String name;

   // 1 ) @Valid(FirstAdress.class) -> The attribute value is undefined for the annotation type Valid
   // 2 ) @Validated(FirstAdress.class) -> The annotation @Validated is disallowed for this location
   @Valid
   private Adress firstAdress;

   // 1 ) @Valid(SecondAdress.class)
   // 2 ) @Validated(SecondAdress.class)
   @Valid
   private Adress secondAdress;

}

And

public class Adress {

   @Size(min = 5, groups = { FirstAdress.class })
   @Size(min = 10, groups = { SecondAdress.class })
   private String city;

   public interface FirstAdress{
   }

   public interface SecondAdress{
   }
}

So, for the object firstAdress, the field city must be at least 5 characters, and for the secondAdress, the city must be at least of 10 characters (just an example).

The solution 1) and 2) in the code are not working/available yet, and I would like to know if what I want to do is possible, or if there is an other way to do it.

Thanks a lot !

Upvotes: 2

Views: 555

Answers (1)

Baakan
Baakan

Reputation: 139

I tried :

@Valid
@ConvertGroup(to = FirstAdress.class)
private Adress firstAdress;

@Valid
@ConvertGroup(to = SecondAdress.class)
private Adress secondAdress;

And for now it seems working ! I will update this post if finally something went wrong with this solution :)

Upvotes: 1

Related Questions