5

I made a custom validation annotation for unique email (When user registers itself, program checks if email is already in database).

Everything works just fine, but when I need to modify user's info and not to create a new one I run into a problem that says "Email is already in use"

Can I somehow turn off only @UniqueEmail validation(I need the others like email pattern and password validation)?

All validation annotations are in the same User bean.

Thank You.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
JSEvgeny
  • 2,550
  • 1
  • 24
  • 38

2 Answers2

2

I'm going to assume that you are using javax.validation.

First you need an interface OnUpdate:

javax.validation.groups.Default

public interface OnUpdate extends Default {}

Now you need to set all the annotations that need to only run on UPDATE:

@NotNull(groups = OnUpdate.class)

Now, you have divided your validations into two groups, those in OnUpdate and those in Default. Running Default will include those in OnUpdate as OnUpdate extends Default.

Now, this is where it gets a little tricky - you need to tell the framework which validations to run on each task.

If using JPA you just need to set the following property:

javax.persistence.validation.group.pre-update = com.my.package.OnUpdate
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Jep thanx! This worked for me! Tried to fix it couple different ways, but this one is the easiest and more elegant :D – JSEvgeny Apr 26 '16 at 13:45
  • 1
    With Spring Boot 2.0.4 the property `javax.persistence.validation.group.pre-update` was not recognized nor necessary. Instead annotate the controller method's parameter with @Validated({OnUpdate.class}), only in the method where you need your custom validation. – Paco Abato Sep 01 '18 at 17:22
  • 2
    @PacoAbato that is incorrect. This answer is about triggering JPA validation. Your comment is about validating incoming requests in Spring MVC. This is very different. – Boris the Spider Sep 01 '18 at 17:24
  • @BoristheSpider Oh, sorry, I mixed concepts that I don't understand yet. Thanks for clarifying. – Paco Abato Sep 01 '18 at 21:53
  • why is it not throwing a bad request like other validations and instead is throwing 500? – ancm Oct 11 '19 at 19:34
  • 1
    @ancm because this validation is at the persistence layer not the mvc layer. If you want to handle it in some way, you’ll need to add that yourself. If you want validation at the mvc layer, then don’t use this method. – Boris the Spider Oct 12 '19 at 20:57
0

Further to Boris The Spider's answer, if you want to do this in code rather than with persistence.xml you can create the following bean:

  @Bean
  public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(final Validator validator) {
    return new HibernatePropertiesCustomizer() {

      @Override
      public void customize(Map<String, Object> hibernateProperties) {
        @SuppressWarnings("rawtypes")
        Class[] classes = {OnUpdate.class};
        hibernateProperties.put("javax.persistence.validation.group.pre-update", classes);
      }
    };
  }
Mr R
  • 289
  • 1
  • 6
  • 14