0

Is there a way to stop some validations to be executed at the time of data persist.

I know about

spring.jpa.properties.javax.persistence.validation.mode=none

but I believe that disables all validations, I just want to disable it for some of the fields (Password specifically, as it is encoded by that time and then pattern doesn't match). Thanks!

Update(More Details):

@Pattern(regexp = "^[A-Za-z0-9_@!]+$")
private String password;

I have a password field validated using the above pattern (A-Z, a-z, 0-9 and _,@,!). In the controller it validates success by below code.

@RequestMapping(value = "/adduser", method = RequestMethod.POST)
public ModelAndView signUp(ModelAndView modelAndView, @ModelAttribute @Valid LoginUser loginUser, BindingResult bindingResult) {
    LoginUser loginUserAdded = null;

    if (!bindingResult.hasErrors()) {
        loginUser.setPassword(passwordEncoder.encode(loginUser.getPassword()));
        loginUserAdded = createUser(loginUser);

....

But then before persist I encode the password and then it throws error while calling save method in JpaRepository because the password value has been changed by the encoder and it doesnt satisfy the pattern validation applied to it.

Now I am looking for a way by which I can disable validation on this field at the time of persist.

Vikash
  • 2,046
  • 3
  • 23
  • 30
  • I can't understand your question. Why do you ask JPA to validate a password with a pattern if you know that the password will not match the pattern, and that is expected? – JB Nizet Sep 10 '17 at 14:14
  • 3
    I don't recommend that you use your entity classes in the controller layer. I always have separate DTO classes for controller input/output, you can read why here, https://stackoverflow.com/questions/43820194/should-i-consider-using-dto-for-spring-rest-controller-layers-instead-of-entitie/43823840#43823840 – Klaus Groenbaek Sep 10 '17 at 14:51
  • 1
    @KlausGroenbaek I agree 100%, but I think you're missing a very important reason: what you receive from the browser is not the same thing as what you save in the database: different information, different structure, different semantics, different types, different validation rules. A plain text password is not the same thing as a hashed password. – JB Nizet Sep 10 '17 at 14:56
  • @JBNizet & KlausGroenbaek . Thanks guys for the link and information!I got your point why a seprate class should be used for this purpose and I will change my application accordingly. Also should I assume that there is no such way to disable one particular validation at persist time ? .. – Vikash Sep 10 '17 at 15:06
  • 1
    No. FYI (but I really don't advise using that trick in this case), you could use the Validated annotation rather than using the Valid annotation, in order to only apply a given validation group when the model attribute is validated by Spring. And specify that group on your Pattern annotation. But again, A dedicated command DTO is a better solution. – JB Nizet Sep 10 '17 at 15:13
  • @JB Nizet true, I could have been more detailed about the differences. In my opinion the difference are between DTO and Entity is so vast that I would never use the same class to mode both. As you mention, data format/validation is one of those differences. – Klaus Groenbaek Sep 10 '17 at 15:15
  • Thanks for saving my time :) ! I realized that a number of future problems will also be solved by separating concerns of DTO and Entity, (y) – Vikash Sep 10 '17 at 15:23

0 Answers0