FileDocCategorySizeDatePackage
BikeValidator.javaAPI DocExample949Sat Apr 23 19:42:28 BST 2005com.springbook

BikeValidator.java

package com.springbook;

import org.springframework.validation.Validator;
import org.springframework.validation.Errors;

public class BikeValidator implements Validator {
    public boolean supports(Class aClass) {
        return aClass.equals(Bike.class);
    }

    public void validate(Object o, Errors errors) {
        Bike bike = (Bike)o;
        if(bike == null) {
            errors.rejectValue("manufacturer", "error.not-specified", null, "Value required.");
        } else {
            System.out.println("VALIDATOR: bike.manufacturer=" + bike.getManufacturer());
            if(bike.getManufacturer() == null || "".equals(bike.getManufacturer()))
                errors.rejectValue("manufacturer", "Value not present.", null, "Manufacturer required.");
            if(bike.getModel() == null || "".equals(bike.getModel()))
                errors.rejectValue("model", "Value not present.", null, "Model is required.");
        }

    }
}