⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validations.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            if (LOG.isDebugEnabled()) {                LOG.debug("validateEmail: Value is not a valid email, "                          + "returning false and adding this error to the "                          + "list " + error);            }            errors.add(error);            return false;        }    }    /**     * Ensure that a field's value is contained in a list of valid values.  A     * blank value is considered valid as well.     *     * @param bean The transfer object containing the field to be validated     * @param action <code>ValidatorAction</code> containing information used     *               to perform the validation and/or constuct an error message     * @param field Object containing the name of the field to be validated     * @param errors List to which any errors should be added     * @return True if the field is valid, false otherwise     */    public static boolean validateValidValues(DTO bean,                                              ValidatorAction action,                                              Field field,                                              List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateValidValues: Validating using this bean "                      + bean);            LOG.debug("validateValidValues: Validating using this action "                      + action);            LOG.debug("validateValidValues: Validating using this field "                      + field);            LOG.debug("validateValidValues: Validating using this list "                      + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateValidValues invoked with null parameter(s)");            throw new IllegalArgumentException("validateValidValues invoked"                                               + " with null parameter(s)");        }        String canonicalId = bean.getCanonicalId();        String fieldName = field.getProperty();        String value = ValidationUtilities.getValueAsString(bean, fieldName);        String label = CanonicalUtilities.getLabel(canonicalId, fieldName);        List validValues =                     CanonicalUtilities.getValidValues(canonicalId, fieldName);        String [] args = new String [] {label, value};        if (LOG.isDebugEnabled()) {            LOG.debug("validateValidValues: Validating field '" + fieldName                      + "' with a value of '" + value + "'");        }        if (Utilities.isBlank(value)) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateValidValues: Field value is blank, "                          + "returning true");            }            return true;        }        if (validValues.contains(value)) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateValidValues: The field contains a "                          + "valid value");            }            return true;        } else {            ValidationError error = new ValidationError(action.getMsg(), args);            if (LOG.isDebugEnabled()) {                LOG.debug("validateValidValues: The field does not contain a"                          + " valid value");            }            errors.add(error);            return false;        }    }    /**     * Ensure that a field's value is a valid number, and is at less than or     * equal to a maximum value.     *     * @param bean The transfer object containing the field to be validated     * @param action <code>ValidatorAction</code> containing information used     *               to perform the validation and/or constuct an error message     * @param field Object containing the name of the field to be validated     * @param errors List to which any errors should be added     * @return True if the field is valid, false otherwise     */    public static boolean validateMaxValue(DTO bean,                                           ValidatorAction action,                                           Field field,                                           List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateMaxValue: Validating using this bean " + bean);            LOG.debug("validateMaxValue: Validating using this action "                      + action);            LOG.debug("validateMaxValue: Validating using this field " + field);            LOG.debug("validateMaxValue: Validating using this list " + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateMaxValue invoked with null parameter(s)");            throw new IllegalArgumentException("validateMaxValue invoked with"                                               + " null parameter(s)");        }        String canonicalId = bean.getCanonicalId();        String fieldName = field.getProperty();        String value = ValidationUtilities.getValueAsString(bean, fieldName);        String label = CanonicalUtilities.getLabel(canonicalId, fieldName);        double maxVal = CanonicalUtilities.getMaxValue(canonicalId, fieldName);        boolean validNumber = true;        boolean validValue = true;        String [] args = new String [] {label, String.valueOf(maxVal)};        if (LOG.isDebugEnabled()) {            LOG.debug("validateMaxValue: Validating field '" + fieldName                      + "' with a value of '" + value + "'");        }        try {            double dValue = Conversions.stringToDecimal(value).doubleValue();            if (dValue > maxVal) {                validValue = false;            } else {                validValue = true;            }        } catch (Exception ex) {            validNumber = false;        } finally {            if (!validNumber) {                String errorNumber = ValidationUtilities.ERROR_INVALID_NUMBER;                ValidationError error = new ValidationError(errorNumber, args);                if (LOG.isDebugEnabled()) {                    LOG.debug("validateMaxValue: The field value is not a"                              + " valid number, adding the following error"                              + " to the list " + error);                }                errors.add(error);            } else if (!validValue) {                ValidationError error = new ValidationError(action.getMsg(),                                                            args);                if (LOG.isDebugEnabled()) {                    LOG.debug("validateMaxValue: The field value is greater"                              + " than the maximum value of '" + maxVal                              + "', adding the following error to the list "                              + error);                }                errors.add(error);            }        }        boolean success = validNumber && validValue;        if (success) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateMaxValue: The field value is less than or"                          + " equal to " + maxVal + ", returning true");            }        } else {            if (LOG.isDebugEnabled()) {                LOG.debug("validateMaxValue: The field value is invalid, "                          + "returning false");            }        }        return success;    }    /**     * Ensure that a field's value is a valid number, and is greater than or     * equal to a minumum value.     *     * @param bean The transfer object containing the field to be validated     * @param action <code>ValidatorAction</code> containing information used     *               to perform the validation and/or constuct an error message     * @param field Object containing the name of the field to be validated     * @param errors List to which any errors should be added     * @return True if the field is valid, false otherwise     */    public static boolean validateMinValue(DTO bean,                                           ValidatorAction action,                                           Field field,                                           List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateMinValue: Validating using this bean " + bean);            LOG.debug("validateMinValue: Validating using this action "                      + action);            LOG.debug("validateMinValue: Validating using this field " + field);            LOG.debug("validateMinValue: Validating using this list " + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateMinValue invoked with null parameter(s)");            throw new IllegalArgumentException("validateMinValue invoked with"                                               + " null parameter(s)");        }        String canonicalId = bean.getCanonicalId();        String fieldName = field.getProperty();        String value = ValidationUtilities.getValueAsString(bean, fieldName);        String label = CanonicalUtilities.getLabel(canonicalId, fieldName);        double minVal = CanonicalUtilities.getMinValue(canonicalId, fieldName);        boolean validNumber = true;        boolean validValue = true;        String [] args = new String [] {label, String.valueOf(minVal)};        if (LOG.isDebugEnabled()) {            LOG.debug("validateMinValue: Validating field '" + fieldName                      + "' with a value of '" + value + "'");        }        try {            double dValue = Conversions.stringToDecimal(value).doubleValue();            if (dValue < minVal) {                validValue = false;            } else {                validValue = true;            }        } catch (Exception ex) {            validNumber = false;        } finally {            if (!validNumber) {                String errorNumber = ValidationUtilities.ERROR_INVALID_NUMBER;                ValidationError error = new ValidationError(errorNumber, args);                if (LOG.isDebugEnabled()) {                    LOG.debug("validateMinValue: The field value is not a"                              + " valid number, adding the following error to"                              + " the list " + error);                }                errors.add(error);            } else if (!validValue) {                ValidationError error = new ValidationError(action.getMsg(),                                                            args);                if (LOG.isDebugEnabled()) {                    LOG.debug("validateMinValue: The field value is less than"                              + " the minimum value of '" + minVal + "', "                              + "adding the following error to the list "                              + error);                }                errors.add(error);            }        }        boolean success = validNumber && validValue;        if (success) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateMinValue: The field value is greater than"                          + " or equal to " + minVal + ", returning true");            }        } else {            if (LOG.isDebugEnabled()) {                LOG.debug("validateMinValue: The field value is invalid, "                          + "returning false");            }        }        return success;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -