Announcement Announcement Module
Collapse
No announcement yet.
Custom validator together with JSR 303 annotation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom validator together with JSR 303 annotation

    Hi,
    I have the scenario where I want to use JSR 303 annotation for validation but also trigger a custom validator for more complex validation rules. I have read through the following post http://forum.springsource.org/showth...ght=%40NotNull but still can't managed to get it working.

    I have the following code:

    Bean snippet:
    Code:
    public class User {
       
       @NotEmpty
       @Size(min=1, max=25)
       private String firstName;
    
       @Email
       private String email;
    }
    Controller (snippet)
    Code:
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
       binder.setValidator(new UserValidator());
    }
    
    @RequestMapping(value="/create", method = RequestMethod.POST)
        public String add(@Valid User user, BindingResult result) {
            if (result.hasErrors()) {
                return "/cms/admin/createUser";
            }
            this.users.put(user.getUserName(), user);
            return "redirect:/cms/admin/users";
        }
    And validator:
    Code:
    @Component
    public class UserValidator extends LocalValidatorFactoryBean implements Validator {
    
    	private final Logger logger = LoggerFactory.getLogger(getClass());
    	
    	@Override
    	public boolean supports(Class<?> clazz) {
    		return User.class.isAssignableFrom(clazz);
    	}
    
    	@Override
    	public void validate(Object target, Errors errors) {
    		super.validate(target, errors);
    		User user = (User) target;
    		if (user != null) {
    			if (logger.isDebugEnabled()) {
    				logger.debug("Validate email with value " + user.getEmail());
    			}
    			if (!StringUtils.hasLength(user.getEmail())) {
    				errors.rejectValue("email", "required", "required");
    			}
    		} else {
    			errors.reject("User object not available");
    		}
    	}
    
    }
    The annotation validation rules works by them self (using hibernate validation) but the call to super.validate(target, errors); in my registered validator throws a NPE in the SpringValidatorAdapter class on the line:
    Code:
    Set<ConstraintViolation<Object>> result = this.targetValidator.validate(target);
    So it seems that targetValidator is null. I have added
    Code:
    <mvc:annotation-driven />
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
    to my configuration file and hibernate and javax.validation is on my classpath.

    Does anyone have any ideas what I missed to configure? Oh and I am using Spring 3.0.2.RELEASE.

    Regards
    Andreas

  • #2
    I encountered the same problem as you Andreas.
    It worked for meby not instantiating your own validator but just autowire it like the following

    Code:
    @Autowired
    private Validator userValidator;
    
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
       binder.setValidator(userValidator);
    }

    Comment


    • #3
      Thanks for that adding the bean to the config file and autowire it as you explained works for me as well.

      Regards
      Andreas

      Comment

      Working...
      X