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 a field has a valid e-mail address.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed./*from   w w w . j ava  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 True if valid, false otherwise.
 */
public static boolean validateEmail(Object bean, ValidatorAction va, Field field, Errors errors) {

    String value = extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isEmail(value)) {
        rejectValue(errors, field, va);
        return false;
    } else {
        return true;
    }
}

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

/**
 * Checks if the field's length is greater than or equal to the minimum
 * value. A <code>Null</code> will be considered an error.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed./*from  ww w.  j av 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 True if stated conditions met.
 */
public static boolean validateMinLength(Object bean, ValidatorAction va, Field field, Errors errors) {

    String value = extractValue(bean, field);

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

            if (!GenericValidator.minLength(value, min)) {
                rejectValue(errors, field, va);

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

    return true;
}

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

/**
 * Checks if the field isn't null and length of the field is greater than zero not including whitespace.
 *
 * @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 meets stated requirements, <code>false</code> otherwise.
 *//* w  w  w .  ja va 2  s  .  c  o m*/
public static boolean validateRequired(Object bean, ValidatorAction va, Field field, Errors errors) {

    String value = FieldChecks.extractValue(bean, field);

    if (GenericValidator.isBlankOrNull(value)) {
        FieldChecks.rejectValue(errors, field, va);
        return false;
    } else {
        return true;
    }

}

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

/**
 * Checks if the field isn't null based on the values of other fields.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed./*  w  w  w.  j ava2s. co  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 validator The <code>Validator</code> instance, used to access other
 * field values.
 * -param request
 * Current request object.
 * @return true if meets stated requirements, false otherwise.
 */
public static boolean validateRequiredIf(Object bean, ValidatorAction va, Field field, Errors errors,
        org.apache.commons.validator.Validator validator) {

    Object form = validator.getParameterValue(org.apache.commons.validator.Validator.BEAN_PARAM);

    boolean required = false;

    String value = FieldChecks.extractValue(bean, field);

    int i = 0;
    String fieldJoin = "AND";
    if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
        fieldJoin = field.getVarValue("fieldJoin");
    }

    if (fieldJoin.equalsIgnoreCase("AND")) {
        required = true;
    }

    while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
        String dependProp = field.getVarValue("field[" + i + "]");
        String dependTest = field.getVarValue("fieldTest[" + i + "]");
        String dependTestValue = field.getVarValue("fieldValue[" + i + "]");
        String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");

        if (dependIndexed == null) {
            dependIndexed = "false";
        }

        String dependVal = null;
        boolean thisRequired = false;
        if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
            String key = field.getKey();
            if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
                String ind = key.substring(0, key.indexOf(".") + 1);
                dependProp = ind + dependProp;
            }
        }

        dependVal = ValidatorUtils.getValueAsString(form, dependProp);
        if (dependTest.equals(FieldChecks.FIELD_TEST_NULL)) {
            thisRequired = (dependVal != null) && (dependVal.length() > 0);
        }

        if (dependTest.equals(FieldChecks.FIELD_TEST_NOTNULL)) {
            thisRequired = (dependVal != null) && (dependVal.length() > 0);
        }

        if (dependTest.equals(FieldChecks.FIELD_TEST_EQUAL)) {
            thisRequired = dependTestValue.equalsIgnoreCase(dependVal);
        }

        if (fieldJoin.equalsIgnoreCase("AND")) {
            required = required && thisRequired;
        } else {
            required = required || thisRequired;
        }

        i++;
    }

    if (required) {
        if (GenericValidator.isBlankOrNull(value)) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        } else {
            return true;
        }
    }
    return true;
}

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

/**
 * Checks if the field matches the regular expression in the field's mask
 * attribute./*from  w ww  .j  a  va 2s.co 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 field matches mask, false otherwise.
 */
public static boolean validateMask(Object bean, ValidatorAction va, Field field, Errors errors) {
    String mask = field.getVarValue("mask");
    String value = FieldChecks.extractValue(bean, field);
    try {
        if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, mask)) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        } else {
            return true;
        }
    } catch (Exception e) {
        FieldChecks.log.error(e.getMessage(), e);
    }
    return true;
}

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

/**
 * Checks if the field can safely be converted to a byte primitive.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed./*from www  .ja va  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 Byte if valid, null otherwise.
 */
public static Byte validateByte(Object bean, ValidatorAction va, Field field, Errors errors) {

    Byte result = null;
    String value = FieldChecks.extractValue(bean, field);

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

From source file:org.springmodules.validation.commons.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 w  ww.j av a 2s . 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 Short if valid, otherwise null.
 */
public static Short validateShort(Object bean, ValidatorAction va, Field field, Errors errors) {
    Short result = null;

    String value = FieldChecks.extractValue(bean, field);

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

From source file:org.springmodules.validation.commons.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 a  v a  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 An Integer if valid, a null otherwise.
 */
public static Integer validateInteger(Object bean, ValidatorAction va, Field field, Errors errors) {
    Integer result = null;

    String value = FieldChecks.extractValue(bean, field);

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

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

    return result;
}

From source file:org.springmodules.validation.commons.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.//from   w  ww  .  ja  v  a2  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 A Long if valid, a null otherwise.
 */
public static Long validateLong(Object bean, ValidatorAction va, Field field, Errors errors) {
    Long result = null;

    String value = FieldChecks.extractValue(bean, field);

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

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

From source file:org.springmodules.validation.commons.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.j  a  v a  2s . 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 A Float if valid, a null otherwise.
 */
public static Float validateFloat(Object bean, ValidatorAction va, Field field, Errors errors) {
    Float result = null;
    String value = FieldChecks.extractValue(bean, field);

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

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

    return result;
}