Manually call Spring Annotation Validation

后端 未结 6 1706
太阳男子
太阳男子 2020-11-29 02:42

I\'m doing a lot of our validation with Hibernate and Spring Annotations like so:

public class Account {
    @NotEmpty(groups = {Step1.class, Step2.class})
          


        
6条回答
  •  情书的邮戳
    2020-11-29 03:17

    Going a step further than Jaiwo99 in his answer:

    // org.springframework.validation.SmartValidator - implemented by
    //     LocalValidatorFactoryBean, which is funny as it is not a FactoryBean per se (just saying)
    @Autowired
    SmartValidator validator;
    
    public String saveAccount(@ModelAttribute Account account, BindingResult result) {
        // ... custom logic
        validator.validate(account, result, Account.Step1.class);
        if (result.hasErrors()) {
            // ... on binding or validation errors
        } else {
            // ... on no errors
        }
        return "";
    }
    

    And the mandatory link to SmartValidator JavaDoc if you are interested.

提交回复
热议问题