1

I have an application where I can create and edit groups. Every group has a unique title. When someone tries to create new group, I check whether there is a group with that title and if so validation fails. The problem is when someone tries to edit group and does not change the title, according to my custom validation there is one group with that title (the one someone is currently editing) and my validation fails, thus user can not save the group. So I would like to ask if anyone might have any solution to this problem. Many thanks for any answer!

My group class

public class UserGroupData implements Serializable {

private Long id;
@NotNull(message = "{NotNull.userGroupData.title}")
@Size(max = 20, message = "{Size.userGroupData.title}")
@UserGroupTitle(message = "{Unique.userGroupData.title}")
private String title;

My validator interface

    @Documented
@Constraint(validatedBy = UserGroupTitleValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface UserGroupTitle {


    String message();

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

My validator

public class UserGroupTitleValidator implements ConstraintValidator<UserGroupTitle, String> {
    @Autowired
    UserGroupRepo userGroupRepo;

    @Override
    public void initialize(UserGroupTitle paramA) {
    }

    @Override
    public boolean isValid(String UserGroupTitle, ConstraintValidatorContext ctx) {
        UserGroup userGroup = userGroupRepo.findOneByTitle(UserGroupTitle);

        if(userGroup!=null){
            return false;
        }
        return true;
    }
}
Deacon
  • 3,615
  • 2
  • 31
  • 52
Marek Sekyra
  • 175
  • 2
  • 11

1 Answers1

0

Assuming that you are using JPA (at least you tagged the question with it) and the problem occurs when the entity gets validated as part of the Bean Validation integration with JPA, you can configure which groups gets validated per JPA life cycle type. For this to work you need to assign a group for your UserGroupValidation:

public class UserGroupData implements Serializable {

   private Long id;

   @NotNull(message = "{NotNull.userGroupData.title}")
   @Size(max = 20, message = "{Size.userGroupData.title}")
   @UserGroupTitle(message = "{Unique.userGroupData.title}", groups = AtInsertOnly.class)
   private String title;
}

public interface AtInsertOnly {
}

Next you need to tell JPA which groups to target during validation. Per default only the Default group is targeted. But via thejavax.persistence.validation.group.pre-persist, javax.persistence.validation.group.pre-update and javax.persistence.validation.group.pre-remove you can configure which groups gets validated per life cycle event type. In your case you would set javax.persistence.validation.group.pre-persist to javax.validation.Default, <mypackage>.AtInsertOnly.

These properties should be added to your persistence.xml.

Hardy
  • 18,659
  • 3
  • 49
  • 65
  • Many thanks for your answer, but I have found sort of different solution. I reffered to this answer [link](http://stackoverflow.com/questions/9284450/jsr-303-validation-if-one-field-equals-something-then-these-other-fields-sho) The problem was that I needed to pass another value into my validator, ie ID, then validation is easy. – Marek Sekyra May 25 '15 at 08:08