Skip to content

MethodValidationPostProcessor using multiple (custom) validators [SPR-12563] #17164

Closed

Description

@spring-projects-issues

Sanjaya Kumar Patel opened SPR-12563 and commented

Currently MethodValidationPostProcessor validates using only one pre-injected validatior. Instead, it could be nice to let it use multiple validators. For example, say developers could annotate service classes with @Validated("aCustomValidatorBeanId"), and then the MethodValidationBeanPostProcessor can use the designated validator. See reference url for more.


Affects: 4.1.3

Reference URL: http://stackoverflow.com/questions/27222939/how-to-use-methodvalidationpostprocessor-when-having-multiple-custom-validato

Issue Links:

Activity

  1. spring-projects-issues commented on Feb 24, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    It turns out that overriding the validator that way is a bit weird since MethodValidationPostProcessor works with JSR-303, really.

    In other words, the interceptor calls Validator.forExecutables().validateParameters which would be quite awkward to extend.

    I have tried a few things and none are satisfactory. Can you revisit your use case and let me know what you're trying to do exactly? In particular, pay attention that your example extends a different facade in LocalValidatorFactoryBea that's not even being called in the method validation code path.

  2. spring-projects-issues commented on Feb 26, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sanjaya Kumar Patel commented

    I write different custom validators, for different forms, like this one below for checking for duplicate emails in the SignupForm:

    @Component
    public class SignupFormValidator extends LocalValidatorFactoryBean {
    	
    	private UserRepository userRepository;
    	
    	@Resource
    	public void setUserRepository(UserRepository userRepository) {
    		this.userRepository = userRepository;
    	}
    
    	@Override
    	public boolean supports(Class<?> clazz) {
    		return clazz.isAssignableFrom(SignupForm.class);
    	}
    
    	@Override
    	public void validate(Object obj, Errors errors, final Object... validationHints) {
    		
    		super.validate(obj, errors, validationHints);
    		
    		if (!errors.hasErrors()) {
    			SignupForm signupForm = (SignupForm) obj;
    			User user = userRepository.findByEmail(signupForm.getEmail());
    			if (user != null)
    				errors.rejectValue("email", "emailNotUnique");			
    		}
    		
    	}
    
    }

    I use these in the controllers, by binding these like this:

    @InitBinder("signupForm")
    protected void initSignupBinder(WebDataBinder binder) {
         binder.setValidator(signupFormValidator);
    }

    And, my controller's handler method looks like this:

    	@RequestMapping(value = "/signup", method = RequestMethod.POST)
    	public String signup(@ModelAttribute("signupForm") @Valid SignupForm signupForm,
    			BindingResult result, RedirectAttributes redirectAttributes) {
    		
    		if (result.hasErrors())
    			return "signup";
    		
    		userService.signup(signupForm);
    		
    		MyUtil.flash(redirectAttributes, "success", "signupSuccess");
    		
    		return "redirect:/";
    
    	}

    But I want to move the validations to the service classes so that they could be called from other layers. So, I was thinking how to do that using MethodValidationPostProcessor.

  3. spring-projects-issues commented on Feb 26, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    Like we said already, MethodValidationPostProcessor is JSR303 validation, it won't call your validate method at all. It looks like you're looking for a variant of that BPP that would work with Spring variant, am I right?

  4. spring-projects-issues commented on Feb 26, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sanjaya Kumar Patel commented

    I see. I'm sorry I could not make out what you meant by the "BPP."

    So, looks like I'll have to migrate these custom validators to custom JSR-303 annotations, or call these manually in the service classes, as I can think.

  5. added
    status: waiting-for-triage
    type: enhancement
    in: core
    and removed
    type: enhancement
    on Jan 11, 2019
  6. added
    status: bulk-closed
    and removed
    status: bulk-closed
    status: waiting-for-triage
    on Jan 11, 2019
  7. spring-projects-issues commented on Jan 12, 2019

    @spring-projects-issues
    CollaboratorAuthor

    Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: corestatus: bulk-closed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions