litesoft
@ 724
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / shared / validators / MaxLengthValueValidator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.temp.shared.validators; import org.litesoft.core.util.externalization.*; import com.temp.shared.*; import com.temp.shared.utils.*; public class MaxLengthValueValidator implements ValueValidator { public static final String TEMPLATE_ID_CODE = "TooLong"; public static final String USER_DATA_MAX_LENGTH = "maxLength"; public static final String USER_DATA_CURRENT_LENGTH = "currentLength"; private final int maxLength; public MaxLengthValueValidator( int maxLength ) { this.maxLength = Validate.length( "maxLength", maxLength ); } @Override public E13nData checkValue( Object value ) { if ( value instanceof String ) { return checkString( value.toString().trim() ); } return null; // OK! } protected E13nData checkString( String userData ) { if ( userData.length() > maxLength ) { return new E13nData( TEMPLATE_ID_CODE )// .addSubstitutionNamedUserData( USER_DATA_MAX_LENGTH, Integer.toString( maxLength ) ) // .addSubstitutionNamedUserData( USER_DATA_CURRENT_LENGTH, Integer.toString( userData.length() ) ); } return null; // OK! } } |