StringLengthValidator

public class StringLengthValidator extends BaseValidator implements GracieValidator {
    
    /**
     * Return true when the input text length is in the limitation
     * @param inputText         Input string text
     * @param maxLength         Max length for the input text
     * @return boolean          Return true when the input text length is in the limitation,
     *                          otherwise return false.
     */
    public static boolean validate(String inputText, int maxLength) {
        boolean isValid = false;
        
        if (!Util.isEmpty(inputText)) {
            int length = inputText.length();
            
            if (length <= maxLength) {
                isValid = true;
            }
        } else {
            isValid = true;
        }
        
        return isValid;
    }
    
    /**
     * Return true when the input text length is in the limitation
     * @param inputText             Input string text
     * @param maxLength             Max length for the input text
     * @param fieldErrorParam       Field error tag parameter
     * @param fieldErrorMessage     Field error message
     * @return boolean              Return true when the input text length is in the limitation,
     *                              otherwise return false.
     */
    public static boolean validate(String inputText, int maxLength, String fieldErrorParam, String fieldErrorMessage) {
        boolean isValid = StringLengthValidator.validate(inputText, maxLength);
        
        if (!isValid) {
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    /**
     * Return true when the input text length is in the limitation
     * @param inputText          Input string text
     * @param maxLength          Max length for the input text
     * @param fieldErrorParam    Field error tag parameter
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     * @return boolean           Return true when the input text length is in the limitation,
     *                           otherwise return false.
     */
    public static boolean validate(String inputText, int maxLength, String fieldErrorParam, 
            String bundle, String key, String defaultMessage, String[] arguments) {
        boolean isValid = StringLengthValidator.validate(inputText, maxLength);
        
        if (!isValid) {
            String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }

	public boolean isValid(String inputText, int maxLength) {
		return validate(inputText,maxLength);
	}

	public boolean isValid(String inputText, Object[] objs) {
		int maxLength = Integer.parseInt((String) objs[0]);
		return validate(inputText,maxLength);
	}

	public boolean isValid(String inputText, String fieldErrorParam, String bundle, String key, String defaultMessage, String[] arguments) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isValid(String inputText, String fieldErrorParam, String fieldErrorMessage) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isValid(String inputText) {
		// TODO Auto-generated method stub
		return false;
	}
    
    

}

 

你可能感兴趣的:(StringLengthValidator)