Results 1 to 2 of 2

Thread: Custom validation in Spring MVC 3

  1. #1
    Join Date
    May 2007
    Posts
    1

    Question Custom validation in Spring MVC 3

    Hi folks,

    I've read through tutorials and docs pertaining to validation in Spring MVC 3 with the implementation of JSR303. No doubt using annotations is handy; what about custom validation required on a field? Can I specify a method or class where I can implement custom code to validate a username value against the DB instead of plainly checking for null and its length using annotations?


    Thanks.

  2. #2

    Default the way I do it...

    Basicly I create a validator for each class that I need to custom validate.
    In this case Im forcing users to select something from a AcademicDegree dropdown list id = -1 means the user hasnt selected anything.

    Hope this helps.


    Code:
    public class AcademicDegreeValidator implements Validator {
    
    	@Override
    	public boolean supports(Class clazz) {
    		return AcademicDegree.class.equals(clazz);
    	}
    
    	@Override
    	public void validate(Object obj, Errors e) {
    		//ValidationUtils.rejectIfEmpty(e, "AcademicDegree", "AcademicDegree.empty");
    		AcademicDegree a = (AcademicDegree) obj;
    		if(a.getId() < 1L){
    			e.rejectValue("academicDegree", "academic_degree_not_selected_error_message");
    		}
    			
    		
    	}
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •