Results 1 to 7 of 7

Thread: Validator not being called

Threaded View

  1. #1
    Join Date
    Apr 2010
    Posts
    5

    Default Validator not being called

    Hi,

    I'm having some difficulties with Spring MVC 3 annotated @Controller.

    My controller looks like:
    Code:
        @Controller
        @RequestMapping("/da/search")
        public class SearchController {
    
        @Autowired
        @Qualifier("searchFormValidator")
        private Validator searchFormValidator;
    
    
        @InitBinder("searchForm")
        public void initBinder(WebDataBinder binder) {
        	// allowed fields (to prevent malicious users posting additional 
        	binder.setAllowedFields(SearchForm.getAllowedFields());
        	
        	// set required fields
        	binder.setRequiredFields(SearchForm.getRequiredFields());
        	
        	// set date format binding
        	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        	binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
        	
        	// set timeperiod binding
        	binder.registerCustomEditor(TimePeriod.class, new TimePeriodEditor());
    
        	// set validator to use for logical errors
        	binder.setValidator(searchFormValidator);
        }
    
    
        @RequestMapping(method=RequestMethod.GET)
        public String initSearch(Model model) {
        	SearchForm searchForm = new SearchForm();
        	searchForm.setPeriod(TimePeriod.LATEST);
        	model.addAttribute("searchForm", searchForm);
        	return "search";
        }
    
    
        @RequestMapping(method=RequestMethod.POST)
        public String performSearch(@ModelAttribute("searchForm") SearchForm searchForm, BindingResult result) {
    		
    		if (result.hasErrors()) {
                            .
                            .
    
    		}
    		return "search";
        }
    The validator is autowired and defined in my application-context.xml.

    When I debug I can see that initBinder is called for when I call /da/search.do with HTTP GET. Also, the supports-method of my Validator is called.

    When I submit the form (HTTP POST) initBinder is called and also supports but not validate on my validator... shouldn't it be called as well? Or do I have to invoke myself? If so, why would I bother to use the "setValidator" in initBinder-method?
    Last edited by stajo917; Apr 22nd, 2010 at 07:52 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •