All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mod4j.runtime.validation.MinLengthValidator Maven / Gradle / Ivy

Go to download

This module contains a small number of Java classes and Spring configuration files used in applications generated by mod4j.

There is a newer version: 1.3.0
Show newest version
package org.mod4j.runtime.validation;

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

/**
 * @author Eric Jan Malotaux
 */
public class MinLengthValidator implements Validator {
    private String field;

    private int min;

    private Class clazz;

    /**
     * @param field
     * @param max
     */
    public MinLengthValidator(Class clazz, String field, int min) {
        this.clazz = clazz;
        this.field = field;
        this.min = min;
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public boolean supports(Class clazz) {
        return this.clazz.isAssignableFrom(clazz);
    }

    /**
     * {@inheritDoc}
     */
    public void validate(Object target, Errors errors) {
        String value = (String) errors.getFieldValue(field);
        if (value != null) {
            int length = value.length();
            if (length < min) {
                errors.rejectValue(field, "field.length.min", new Integer[] { new Integer(min), new Integer(length) },
                        field + " should be at least " + min + " long, but was " + length);
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy