Announcement Announcement Module
Collapse
No announcement yet.
Validator not being called Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 07:52 AM.

  • #2
    Post your validator as well...

    Comment


    • #3
      Of course - assuming it is correct was maybe a little stupid of me... =)

      But as my breakpoint in the validate-method isn't triggered didn't think the problem was there.

      Thanks for the prompt reply!

      Code:
      public class SearchFormValidator implements Validator {
      
      	public boolean supports(Class<?> clazz) {
      		System.out.println("SearchFormValidator.supports");
      		return SearchForm.class.equals(clazz);
      	}
      
      	public void validate(Object obj, Errors errors) {
      		System.out.println("SearchFormValidator.validate");
      
                      SearchForm searchForm = (SearchForm)obj;
      
              
                      // from & to - from must be before to
                     Calendar from = Calendar.getInstance();
                     from.setTime(searchForm.getFrom());
                     Calendar to = Calendar.getInstance();
                     to.setTime(searchForm.getTo());
                     if (!from.before(to)) {
              	   errors.rejectValue(SearchForm.Field.FROM.getName(), "error.from.before.to",
      					new Object[] { new DefaultMessageSourceResolvable(
      							new String[] { "label.period.from" }) }, "");
              	
              }
      	}
      
      }

      Comment


      • #4
        The supports method is called when registering the validator (at least that is what I see in the code). Also make sure you put @Valid to the method argument you want to validate.

        Comment


        • #5
          Okej, so the supports method is called when the validator is registred - but when is the validate-method called?

          I can't use @Valid as JSR-303 is not available for me in the target environment.

          Comment


          • #6
            @Valid also works without jsr303... It does detection on what is and isn't available.

            Comment


            • #7
              Aha, will test it first thing tomorrow!

              Thanks for the reply!

              Comment

              Working...
              X