Example usage for org.apache.commons.validator Field getProperty

List of usage examples for org.apache.commons.validator Field getProperty

Introduction

In this page you can find the example usage for org.apache.commons.validator Field getProperty.

Prototype

public String getProperty() 

Source Link

Document

Gets the property name of the field.

Usage

From source file:test.valid.TestTypeValidator.java

/**
 * Checks if the field can be successfully converted to a <code>short</code>.
 *
 * @param value The value validation is being performed on.
 * @return boolean If the field can be successfully converted 
 * to a <code>short</code> <code>true</code> is returned.  
 * Otherwise <code>false</code>.
 *//*from  ww  w .  j  a v a2  s  . co  m*/
public static Short validateShort(Object bean, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    return GenericTypeValidator.formatShort(value);
}

From source file:test.valid.TestTypeValidator.java

/**
 * Checks if the field can be successfully converted to a <code>int</code>.
 *
 * @param value The value validation is being performed on.
 * @return boolean If the field can be successfully converted 
 * to a <code>int</code> <code>true</code> is returned.  
 * Otherwise <code>false</code>.
 *//*w  w w  .j  a v  a 2s .  c o  m*/
public static Integer validateInt(Object bean, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    return GenericTypeValidator.formatInt(value);
}

From source file:test.valid.TestTypeValidator.java

/**
 * Checks if the field can be successfully converted to a <code>long</code>.
 *
 * @param value The value validation is being performed on.
 * @return boolean If the field can be successfully converted 
 * to a <code>long</code> <code>true</code> is returned.  
 * Otherwise <code>false</code>.
 *///from  ww  w .  ja va 2s .c  o  m
public static Long validateLong(Object bean, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    return GenericTypeValidator.formatLong(value);
}

From source file:test.valid.TestTypeValidator.java

/**
 * Checks if the field can be successfully converted to a <code>float</code>.
 *
 * @param value The value validation is being performed on.
 * @return boolean If the field can be successfully converted 
 * to a <code>float</code> <code>true</code> is returned.  
 * Otherwise <code>false</code>.
 *//*from ww  w  .  java2s .  c  om*/
public static Float validateFloat(Object bean, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    return GenericTypeValidator.formatFloat(value);
}

From source file:test.valid.TestTypeValidator.java

/**
 * Checks if the field can be successfully converted to a <code>double</code>.
 *
 * @param value The value validation is being performed on.
 * @return boolean If the field can be successfully converted 
 * to a <code>double</code> <code>true</code> is returned.  
 * Otherwise <code>false</code>.
 *//* w w w . j  av  a 2s  .c o m*/
public static Double validateDouble(Object bean, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    return GenericTypeValidator.formatDouble(value);
}

From source file:ubic.gemma.web.util.ValidationUtil.java

/**
 * Validates that a field has a length in some range. This goes with the custom declaration in the
 * validation-rules-custom.xml file./*from  ww  w. j a va  2s  .  c o  m*/
 * 
 * @param bean
 * @param va
 * @param field
 * @param errors
 * @return boolean
 * @author pavlidis
 */
public static boolean validateLengthRange(Object bean, ValidatorAction va, Field field, Errors errors) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String minLengthS = field.getVarValue("minlength");
    String maxLengthS = field.getVarValue("maxlength");

    assert minLengthS != null && maxLengthS != null : "Length variables names aren't valid for field";

    if (!GenericValidator.isBlankOrNull(value)) {
        try {

            int minLength = Integer.parseInt(minLengthS);
            int maxLength = Integer.parseInt(maxLengthS);

            if (value.length() < minLength || value.length() > maxLength) {
                FieldChecks.rejectValue(errors, field, va);
                return false;
            }
        } catch (Exception e) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        }
    }

    return true;
}

From source file:ubic.gemma.web.util.ValidationUtil.java

/**
 * Check that a value is an integer greater than zero.
 * //from  w w  w  .j  a  v  a 2 s  .c o m
 * @param bean
 * @param va
 * @param field
 * @param errors
 * @return
 */
public static boolean validatePositiveNonZeroInteger(Object bean, ValidatorAction va, Field field,
        Errors errors) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    try {
        int v = Integer.parseInt(value);
        if (v <= 0) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        }
    } catch (Exception e) {
        FieldChecks.rejectValue(errors, field, va);
        return false;
    }

    return true;
}

From source file:us.mn.state.health.lims.common.util.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.//from  w w w .  ja va  2 s . 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>ActionMessages</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 valid, false otherwise.
 */
public static Object validateDate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    Object result = null;
    String value = null;
    if (isString(bean)) {
        value = (String) bean;
    } else {
        value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    }
    String datePattern = field.getVarValue("datePattern");
    String datePatternStrict = field.getVarValue("datePatternStrict");
    Locale locale = RequestUtils.getUserLocale(request, null);

    // this is added to use message key
    String datePatternForResources = ResourceLocator.getInstance().getMessageResources().getMessage(locale,
            datePattern);
    if (datePatternForResources != null) {
        datePattern = datePatternForResources;
    }

    datePatternForResources = ResourceLocator.getInstance().getMessageResources().getMessage(locale,
            datePatternStrict);
    if (datePatternForResources != null) {
        datePatternStrict = datePatternForResources;
    }

    if (GenericValidator.isBlankOrNull(value)) {
        return Boolean.TRUE;
    }

    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);
        } else {
            result = GenericTypeValidator.formatDate(value, locale);
        }
    } catch (Exception e) {
        //bugzilla 2154
        LogEvent.logError("FieldChecks", "validateDate()", e.toString());
    }

    if (result == null) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
    }

    return result == null ? Boolean.FALSE : result;
}

From source file:us.mn.state.health.lims.common.util.validator.FieldChecks.java

/**
 * Checks if the field is a valid time HH:MM. 
 * @return true if valid, false otherwise.
 *//*from  www  . j  av a2 s .  c  om*/
public static boolean validateTime(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {

    String value = null;
    if (isString(bean)) {
        value = (String) bean;
    } else {
        value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    }

    try {
        String hours = value.substring(0, 2);
        String minutes = value.substring(3);

        int hh = Integer.parseInt(hours);
        int mm = Integer.parseInt(minutes);

        if (!GenericValidator.isInRange(hh, 0, 23)) {
            errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
            return false;
        } else if (!GenericValidator.isInRange(mm, 0, 59)) {
            errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
            return false;
        } else {
            return true;
        }
    } catch (Exception ex) {
        //bugzilla 2154
        LogEvent.logError("FieldChecks", "validateTime()", ex.toString());
        return false;
    }
}

From source file:us.mn.state.health.lims.common.util.validator.ValidWhen.java

/**
 * Checks if the field matches the boolean expression specified in
 * <code>test</code> parameter.
 * //  w w  w.ja  v a2  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>ActionMessages</code> object to add errors to if
 *            any validation errors occur.
 * 
 * @param request
 *            Current request object.
 * 
 * @return <code>true</code> if meets stated requirements,
 *         <code>false</code> otherwise.
 */
public static boolean validateValidWhen(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {

    Object form = validator.getParameterValue(Validator.BEAN_PARAM);
    String value = null;
    boolean valid = false;
    int index = -1;

    if (field.isIndexed()) {
        String key = field.getKey();

        final int leftBracket = key.indexOf("[");
        final int rightBracket = key.indexOf("]");

        if ((leftBracket > -1) && (rightBracket > -1)) {
            index = Integer.parseInt(key.substring(leftBracket + 1, rightBracket));
        }
    }

    if (isString(bean)) {
        value = (String) bean;
    } else {
        value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    }

    String test = field.getVarValue("test");
    if (test == null) {
        String msg = "ValidWhen Error 'test' parameter is missing for field ' " + field.getKey() + "'";
        errors.add(field.getKey(), new ActionMessage(msg, false));
        //bugzilla 2154
        LogEvent.logError("ValidWhen", "validateValidWhen()", msg);
        return false;
    }

    // Create the Lexer
    ValidWhenLexer lexer = null;
    try {
        lexer = new ValidWhenLexer(new StringReader(test));
    } catch (Exception ex) {
        String msg = "ValidWhenLexer Error for field ' " + field.getKey() + "' - " + ex;
        errors.add(field.getKey(), new ActionMessage(msg + " - " + ex, false));
        //bugzilla 2154
        LogEvent.logError("ValidWhen", "validateValidWhen()", ex.toString());
        LogEvent.logDebug("ValidWhen", "validateValidWhen", msg);
        return false;
    }

    // Create the Parser
    ValidWhenParser parser = null;
    try {
        parser = new ValidWhenParser(lexer);
    } catch (Exception ex) {
        String msg = "ValidWhenParser Error for field ' " + field.getKey() + "' - " + ex;
        errors.add(field.getKey(), new ActionMessage(msg, false));
        //bugzilla 2154
        LogEvent.logError("ValidWhen", "validateValidWhen()", ex.toString());
        LogEvent.logDebug("ValidWhen", "validateValidWhen", msg);
        return false;
    }
    parser.setForm(form);
    parser.setIndex(index);
    parser.setValue(value);

    try {
        parser.expression();
        valid = parser.getResult();

    } catch (Exception ex) {

        // errors.add(
        // field.getKey(),
        // Resources.getActionMessage(validator, request, va, field));

        String msg = "ValidWhen Error for field ' " + field.getKey() + "' - " + ex;
        errors.add(field.getKey(), new ActionMessage(msg, false));
        //bugzilla 2154
        LogEvent.logError("ValidWhen", "validateValidWhen()", ex.toString());
        LogEvent.logDebug("ValidWhen", "validateValidWhen", msg);

        return false;
    }

    if (!valid) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

        return false;
    }

    return true;
}