Souad
Souad

Reputation: 5084

How to validate the Date format in Spring MVC

I have this Validator :

import org.springframework.validation.Validator;

public class AddActivityValidator  implements Validator{

    @Override
    public boolean supports(Class<?> clazz) {

        return Movement.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object obj, Errors err) {
        ValidationUtils.rejectIfEmptyOrWhitespace(err, "mode_affectation", "modeaffectattion.requiered", "Choisissez un mode d'affectation");
        ValidationUtils.rejectIfEmptyOrWhitespace(err, "nom_etabl", "name.required","Choisissez un nom d'une etablissement");
        ValidationUtils.rejectIfEmptyOrWhitespace(err, "ville", "city.required","Choisissez une ville");    
        ValidationUtils.rejectIfEmptyOrWhitespace(err, "delegation", "delegation.required","Choisissez une délégation");
        ValidationUtils.rejectIfEmptyOrWhitespace(err, "cinn", "cinn.required","Il faut choisir un fonctionnaire au dessus avant d'essayer de saisir un service");

    }

}

and I want to validate also the date attribute but the problem is that ValidationUtils Class just check if a field is empty. and I don't find any tutorial how to do something to validate the date. Any idea?

* Edit **

I add this annotation to my attribute in the Class :

@DateTimeFormat(pattern="yyyy-MM-dd")
    String date_debut;

But how to attach a message to this and link it with the controller ?

Upvotes: 0

Views: 6851

Answers (1)

Bassem Reda Zohdy
Bassem Reda Zohdy

Reputation: 12942

Try to enable Hibernate validation JSR303 with Spring MVC.

@Future
@Past

Link

Upvotes: 1

Related Questions