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

📄 validations.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            LOG.debug("validateMaxLength: Validating field '"                      + fieldName + "' with a value of '" + value + "'");        }        if (!GenericValidator.maxLength(value, length)) {            ValidationError error = new ValidationError(action.getMsg(), args);            if (LOG.isDebugEnabled()) {                LOG.debug("validateMaxLength: Value exceeds the maximum"                          + " length of '" + length + "', returning false"                          + " and adding this error to the list " + error);            }            errors.add(error);            return false;        } else {            if (LOG.isDebugEnabled()) {                LOG.debug("validateMaxLength: Value less than or equal to '"                          + length + "' characters long, returning true");            }            return true;        }    }    /**     * Ensure that a field's length is at least a certain number of characters.     *     * @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 validateMinLength(DTO bean,                                            ValidatorAction action,                                            Field field,                                            List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateMinLength: Validating using this bean " + bean);            LOG.debug("validateMinLength: Validating using this action "                      + action);            LOG.debug("validateMinLength: Validating using this field "                      + field);            LOG.debug("validateMinLength: Validating using this list "                      + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateMinLength invoked with null parameter(s)");            throw new IllegalArgumentException("validateMinLength 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);        int length = CanonicalUtilities.getMinLength(canonicalId, fieldName);        String [] args = new String [] {label, String.valueOf(length)};        if (LOG.isDebugEnabled()) {            LOG.debug("validateMinLength: Validating field '"                      + fieldName + "' with a value of '" + value + "'");        }        if (!GenericValidator.minLength(value, length)) {            ValidationError error = new ValidationError(action.getMsg(), args);            if (LOG.isDebugEnabled()) {                LOG.debug("validateMinLength: Value is less than the minimum"                          + " length of '" + length + "', returning false and"                          + " adding this error to the list " + error);            }            errors.add(error);            return false;        } else {            if (LOG.isDebugEnabled()) {                LOG.debug("validateMinLength: Value greater than or equal to '"                          + length + "' characters long, returning true");            }            return true;        }    }    /**     * Ensure that a field contains a valid date value.  A valid date is either     * a value that can be converted to a date by the     * <code>Conversions.stringToDate()</code> method, or a null/empty 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 validateDate(DTO bean,                                       ValidatorAction action,                                       Field field,                                       List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateDate: Validating using this bean " + bean);            LOG.debug("validateDate: Validating using this action " + action);            LOG.debug("validateDate: Validating using this field " + field);            LOG.debug("validateDate: Validating using this list " + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateDate invoked with null parameter(s)");            throw new IllegalArgumentException("validateDate 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);        String [] args = new String [] {label};        if (LOG.isDebugEnabled()) {            LOG.debug("validateDate: Validating field '" + fieldName + "' with"                      + " a value of '" + value + "'");        }        if (Utilities.isBlank(value)) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateDate: Field value is blank, returning true");            }            return true;        }        try {            Conversions.stringToDate(value);            if (LOG.isDebugEnabled()) {                LOG.debug("validateDate: Value is a valid date, returning"                          + " true");            }            return true;        } catch (ConversionException ex) {            ValidationError error = new ValidationError(action.getMsg(), args);            if (LOG.isDebugEnabled()) {                LOG.debug("validateDate: Field is not a valid date, returning"                          + " false and adding this error to the list "                          + error);            }            errors.add(error);            return false;        }    }    /**     * Ensure that a field contains a valid time value.  A valid time is either     * a value that can be converted to a time by the     * <code>Conversions.stringToTime()</code> method, or a null/empty 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 validateTime(DTO bean,                                       ValidatorAction action,                                       Field field,                                       List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateTime: Validating using this bean " + bean);            LOG.debug("validateTime: Validating using this action " + action);            LOG.debug("validateTime: Validating using this field " + field);            LOG.debug("validateTime: Validating using this list " + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateTime invoked with null parameter(s)");            throw new IllegalArgumentException("validateTime 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);        String [] args = new String [] {label};        if (LOG.isDebugEnabled()) {            LOG.debug("validateTime: Validating field '" + fieldName + "' with"                      + " a value of '" + value + "'");        }        if (Utilities.isBlank(value)) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateTime: Field value is blank, returning true");            }            return true;        }        try {            Conversions.stringToTime(value);            if (LOG.isDebugEnabled()) {                LOG.debug("validateTime: Value is a valid time, returning"                          + " true");            }            return true;        } catch (ConversionException ex) {            ValidationError error = new ValidationError(action.getMsg(), args);            if (LOG.isDebugEnabled()) {                LOG.debug("validateTime: Field is not a valid time, returning"                          + " false and adding this error to the list "                          + error);            }            errors.add(error);            return false;        }    }    /**     * Ensure that a field contains a valid date value.  Empty/null values are     * considered to be valid.     *     * @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 validateEmail(DTO bean,                                        ValidatorAction action,                                        Field field,                                        List errors) {        if (LOG.isDebugEnabled()) {            LOG.debug("validateEmail: Validating using this bean " + bean);            LOG.debug("validateEmail: Validating using this action " + action);            LOG.debug("validateEmail: Validating using this field " + field);            LOG.debug("validateEmail: Validating using this list " + errors);        }        if (bean == null || action == null || field == null || errors == null) {            LOG.error("validateEmail invoked with null parameter(s)");            throw new IllegalArgumentException("validateEmail 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);        String [] args = new String [] {label};        if (LOG.isDebugEnabled()) {            LOG.debug("validateEmail: Validating field '" + fieldName                      + "' with a value of '" + value + "'");        }        if (Utilities.isBlank(value)) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateEmail: Field value is blank, returning"                          + " true");            }            return true;        }        EmailValidator validator = EmailValidator.getInstance();        if (validator.isValid(value)) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateEmail: Value is a valid email, returning"                          + " true");            }            return true;        } else {            ValidationError error = new ValidationError(action.getMsg(), args);

⌨️ 快捷键说明

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