문제

I'm using the hibernate validator to validate my forms. I'm having the "problem" that the 9th of the 14th month becomes the second month in the next year. (just en example of a scenario).

I was wondering how I could prevent the default conversion and instead show a custom error message for it.

Does anyone also know how I can display an approperiate message if my custom editor throws an IllegalArgumentException?

@InitBinder
    public void initBinder(WebDataBinder binder) {
        CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true);
        binder.registerCustomEditor(Date.class, editor);
    }

I registred a customEditor because spring-portlet-mvc had some issues with the binding.

도움이 되었습니까?

해결책

This behaviour is controlled by DateFormat.setLenient() and has nothing to do with validation (with setLentient(false) it produces a type mismatch error at the binding phase):

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
df.setLenient(false);
CustomDateEditor editor = new CustomDateEditor(df, true); 
binder.registerCustomEditor(Date.class, editor); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top