John
John

Reputation:

How to validate date using Custom date proprty editor in spring MVC

I have made the Date in in my java Class

and this is the code i have used in controller

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

i am usinng JSR annotations and hibernate to validate other fields.

Is there any way i can use annotations to validate that date must in dd-mm-yyyy format only

Upvotes: 0

Views: 4341

Answers (1)

Costi Ciudatu
Costi Ciudatu

Reputation: 38195

The CustomDateEditor is not a validator itself, but in this case it does implicitly validate your pattern: it will just parse a string to a date using the format you specified. So you'll get a null value if the parsing does not succeed.

Spring validation occurs after binding, so any validation will be performed on the Date object (so after that string is parsed), not on the initial string.

Upvotes: 2

Related Questions