0

I'm developping spring-boot application, I need to validate user information before checking other information. This is a snippet:

@RequestMapping(value = "/create-user", method = RequestMethod.POST)
public String ceateCustomer(@Valid @RequestBody User user){ 
      myService.create(user);
}

When the user comes from another route /create-sp-user, I have to re-valid the user:

@RequestMapping(value = "/create-sp-user", method = RequestMethod.POST)
    public String ceateCustomer(@Valid @RequestBody User user){ 
      user.setSp(true);
      // I tried @Valid user without success
      // In my User class if sp = true the field telNumber is mandatory   
      myService.create(user);
}

Would you have any ideas ?

Best regards

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Olivier
  • 3
  • 3
  • 1
    You can easily write your own custom validator as there is no default valitator that checks for required fields based on the value of others. – Nico Van Belle Jun 18 '17 at 09:54
  • Sorry, I don't get what you want. what's the user structure coming from the client? Or is it, that you want to validate the user **after** you've set sp = true? In that case, you might want to write your custom validator as described here: https://stackoverflow.com/questions/12146298/spring-mvc-how-to-perform-validation - the @valid annotation checks it **before** any code is executed in the controller. – Dominik Jun 18 '17 at 09:55
  • Thanks @Dominik yes I have to validate the user after set sp to true – Olivier Jun 18 '17 at 10:05

1 Answers1

0

Like Dominik said, @Valid annotation validates the object before any code is executed in the controller

If you want to validate the object inside the method you should use custom validator.

Implement org.springframework.validation.Validator and call the validate method inside controller.

@RequestMapping(value = "/create-sp-user", method = RequestMethod.POST)
    public String ceateCustomer(@Valid @RequestBody User user, BindingResult result){ 
      user.setSp(true);
      CustomValidator customValidator = new CustomValidator();
      customValidator.validate(user, result);

      if (result.hasErrors()){
        //do something
      } else {
        myService.create(user);
      }
}
Janar
  • 2,623
  • 1
  • 22
  • 32
  • thanks @Janar I tried you solution but I have all the time 0 errors. – Olivier Jun 20 '17 at 19:40
  • perhaps you implemented the validator wrongly? please post your validator code. – Janar Jun 20 '17 at 20:53
  • Hi @Janar I'm working with @Olivier this is the validator class `public class UserValidator implements Validator { protected Logger logger = LoggerFactory.getLogger(UserValidator.class); @Override public boolean supports(Class> clazz) { return userInfo.class.equals(clazz); } @Override public void validate(Object target, Errors errors) { User userInfo = (User) target; ... // Phone number if (StringUtils.isBlank(userInfo.getPhoneNumber())) { errors.rejectValue("phoneNumber", "phoneNumber[required]", "Failed"); } .. } }` – Victor Jun 21 '17 at 11:36
  • The `supports` method seems to be wrongly implemented - you should check if `User.class.equals(clazz);`. Also try debugging the validate method - is it getting called, is phoneNumber blank when you call it? – Janar Jun 21 '17 at 11:41
  • When I call @Valid User it works but when I call @Valid on class wrapper like `class testWrapper { @Valid private UserInfo u1 , @Valid private UserInfo u2}` – Victor Jun 21 '17 at 12:09
  • You need to validate with your custom validator – Janar Jun 21 '17 at 12:12