org.mod4j.runtime.validation.MaxLengthValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mod4j-runtime-common Show documentation
Show all versions of mod4j-runtime-common Show documentation
This module contains a small number of Java classes and Spring configuration files used in applications generated by
mod4j.
/**
*
*/
package org.mod4j.runtime.validation;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
/**
* @author Eric Jan Malotaux
*/
public class MaxLengthValidator implements Validator {
private String field;
private int max;
private Class clazz;
/**
* @param field
* @param max
*/
public MaxLengthValidator(Class clazz, String field, int max) {
this.clazz = clazz;
this.field = field;
this.max = max;
}
/**
* {@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 > max) {
errors.rejectValue(field, "field.length.max", new Integer[] { new Integer(max), new Integer(length) },
field + " should be at most " + max + " long, but was " + length);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy