Example usage for org.apache.commons.validator GenericValidator isBlankOrNull

List of usage examples for org.apache.commons.validator GenericValidator isBlankOrNull

Introduction

In this page you can find the example usage for org.apache.commons.validator GenericValidator isBlankOrNull.

Prototype

public static boolean isBlankOrNull(String value) 

Source Link

Document

Checks if the field isn't null and length of the field is greater than zero not including whitespace.

Usage

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if the field can safely be converted to a short primitive.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed./*from ww  w. java 2  s.c om*/
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return A Short if valid, otherwise null.
 */
public static Short validateShort(Object bean, ValidatorAction va, Field field, Errors errors) {
    Short result = null;

    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        result = GenericTypeValidator.formatShort(value);
        if (result == null) {
            rejectValue(errors, field, va);
        }
    }
    return result;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if the field can safely be converted to an int primitive.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed./*from  ww w.j  ava2 s . c o m*/
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return An Integer if valid, a null otherwise.
 */
public static Integer validateInteger(Object bean, ValidatorAction va, Field field, Errors errors) {
    Integer result = null;

    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        result = GenericTypeValidator.formatInt(value);

        if (result == null) {
            rejectValue(errors, field, va);
        }
    }

    return result;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if the field can safely be converted to a long primitive.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.// w  ww .  j a v  a  2  s .  c o m
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return A Long if valid, a null otherwise.
 */
public static Long validateLong(Object bean, ValidatorAction va, Field field, Errors errors) {
    Long result = null;

    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        result = GenericTypeValidator.formatLong(value);

        if (result == null) {
            rejectValue(errors, field, va);
        }
    }
    return result;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if the field can safely be converted to a float primitive.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed./*from  w w  w  .ja  v a  2  s.c  o  m*/
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return A Float if valid, a null otherwise.
 */
public static Float validateFloat(Object bean, ValidatorAction va, Field field, Errors errors) {
    Float result = null;
    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        result = GenericTypeValidator.formatFloat(value);

        if (result == null) {
            rejectValue(errors, field, va);
        }
    }

    return result;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if the field can safely be converted to a double primitive.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed./*ww  w. j  a v  a2 s  .c  o  m*/
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return A Double if valid, a null otherwise.
 */
public static Double validateDouble(Object bean, ValidatorAction va, Field field, Errors errors) {
    Double result = null;
    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        result = GenericTypeValidator.formatDouble(value);

        if (result == null) {
            rejectValue(errors, field, va);
        }
    }

    return result;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if the field is a valid date. If the field has a datePattern
 * variable, that will be used to format
 * <code>java.text.SimpleDateFormat</code>. If the field has a
 * datePatternStrict variable, that will be used to format
 * <code>java.text.SimpleDateFormat</code> and the length will be checked
 * so '2/12/1999' will not pass validation with the format 'MM/dd/yyyy'
 * because the month isn't two digits. If no datePattern variable is
 * specified, then the field gets the DateFormat.SHORT format for the
 * locale. The setLenient method is set to <code>false</code> for all
 * variations./*w  w w . j ava2 s  .c o  m*/
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return A Date if valid, a null if blank or invalid.
 */
public static Date validateDate(Object bean, ValidatorAction va, Field field, Errors errors) {

    Date result = null;
    String value = extractValue(bean, field);
    String datePattern = field.getVarValue("datePattern");
    String datePatternStrict = field.getVarValue("datePatternStrict");

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            if (datePattern != null && datePattern.length() > 0) {
                result = GenericTypeValidator.formatDate(value, datePattern, false);
            } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
                result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        if (result == null) {
            rejectValue(errors, field, va);
        }
    }

    return result;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if a fields value is within a range (min &amp; max specified in
 * the vars attribute)./*from www. j a va2 s  .c  o  m*/
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 *
 * @return <code>true</code> if in range, <code>false</code> otherwise.
 */
public static boolean validateIntRange(Object bean, ValidatorAction va, Field field, Errors errors) {
    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            int intValue = Integer.parseInt(value);
            int min = Integer.parseInt(field.getVarValue("min"));
            int max = Integer.parseInt(field.getVarValue("max"));

            if (!GenericValidator.isInRange(intValue, min, max)) {
                rejectValue(errors, field, va);

                return false;
            }
        } catch (Exception e) {
            rejectValue(errors, field, va);
            return false;
        }
    }
    return true;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if a fields value is within a range (min &amp; max specified in
 * the vars attribute)./*from  w ww .j av a2 s .  c  om*/
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return True if in range, false otherwise.
 */
public static boolean validateDoubleRange(Object bean, ValidatorAction va, Field field, Errors errors) {

    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            double doubleValue = Double.parseDouble(value);
            double min = Double.parseDouble(field.getVarValue("min"));
            double max = Double.parseDouble(field.getVarValue("max"));

            if (!GenericValidator.isInRange(doubleValue, min, max)) {
                rejectValue(errors, field, va);

                return false;
            }
        } catch (Exception e) {
            rejectValue(errors, field, va);
            return false;
        }
    }
    return true;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if a fields value is within a range (min &amp; max specified in
 * the vars attribute)./*from  ww w.  ja  v  a 2  s.  c o  m*/
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return True if in range, false otherwise.
 */
public static boolean validateFloatRange(Object bean, ValidatorAction va, Field field, Errors errors) {

    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            float floatValue = Float.parseFloat(value);
            float min = Float.parseFloat(field.getVarValue("min"));
            float max = Float.parseFloat(field.getVarValue("max"));
            if (!GenericValidator.isInRange(floatValue, min, max)) {
                rejectValue(errors, field, va);
                return false;
            }
        } catch (Exception e) {
            rejectValue(errors, field, va);
            return false;
        }
    }

    return true;
}

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if the field is a valid credit card number.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.//from  ww w .  j  a  v a 2  s .com
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return The credit card as a Long, a null if invalid, blank, or null.
 */
public static Long validateCreditCard(Object bean, ValidatorAction va, Field field, Errors errors) {

    Long result = null;
    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        result = GenericTypeValidator.formatCreditCard(value);

        if (result == null) {
            rejectValue(errors, field, va);
        }
    }

    return result;
}