Question

I have a problem with a validation error displayed when i submit my form with an empty date like that in the resulting web page:

Failed to convert property value of type java.lang.String to required type 
java.util.Date for property dateFin; nested exception is 
java.lang.IllegalArgumentException: Could not parse date: Unparseable date: ""

My controller looks like this:

    @Controller
    @SessionAttributes
    @Lazy
    public class MyController extends AbstractMVPAction {
        @RequestMapping(value = "/secured/cp/saveProgram")
        public String enregistrerProgramme(@ModelAttribute Program program,
                 BindingResult bindingResult, ModelMap model){
            if(bindingResult.hasErrors()){
                model.put("program", program);
                return "/secured/cp/showProgram"
            }else{
                // ... saves the programme
                model.put("program", null);
                return "/secured/cp/backToOtherPage"
            }
        }
        @InitBinder
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(Date.class, new CustomDateEditor(
                    new SimpleDateFormat("dd/MM/yyyy"), false));
        }
    }

When I debug my method, I can see my object is fine, the modif I did are well reported, the date is null, but the bindingResult.hasErrors() returns true and according to me it shouldn't.

I used to have some validation annotations in the Program object and a @Valid annotation but I removed them all and still have the problem.

I have read lot's of similar issues and every time the solution is the @InitBinder/CustomDateEditor. So it is there and I guess it's working, the dates are displayed the way I want (this was not the case before I add it) and I can submit them provided it's not empty.

Thank's in advance, I'm starting to go crazy...

Was it helpful?

Solution

You've constructed a CustomDateEditor that explicitly disallows the empty string. Check the Javadoc for the constructor you're using, when the boolean argument is false, passing an empty string to that editor causes the IllegalArgumentException you're seeing.

Try this:

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                new SimpleDateFormat("dd/MM/yyyy"), false));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top