/** * Gets validation mode which is determined by whether min, max, or both values are provided * * @return validation mode */ public final Mode getMode() { final R min = getMinimum(); final R max = getMaximum(); if (min == null && max != null) { return Mode.MAXIMUM; } else if (max == null && min != null) { return Mode.MINIMUM; } else if ((min == null && max == null) || max.equals(min)) { return Mode.EXACT; } else { return Mode.RANGE; } }
/** * Constructor that sets the minimum and maximum values. * * @param minimum * the minimum value * @param maximum * the maximum value */ public AbstractRangeValidator(R minimum, R maximum) { setRange(minimum, maximum); }
@Override public void validate(IValidatable<V> validatable) { R value = getValue(validatable); final R min = getMinimum(); final R max = getMaximum(); if ((min != null && value.compareTo(min) < 0) || (max != null && value.compareTo(max) > 0)) { Mode mode = getMode(); ValidationError error = new ValidationError(this, mode.getVariation()); if (min != null) { error.setVariable("minimum", min); } if (max != null) { error.setVariable("maximum", max); } if (mode == Mode.EXACT) { error.setVariable("exact", max); } validatable.error(decorate(error, validatable)); } }
@Override protected IValidationError decorate(IValidationError error, IValidatable<String> validatable) { error = super.decorate(error, validatable); if (error instanceof ValidationError) { ((ValidationError)error).setVariable("length", validatable.getValue().length()); } return error; }
@Override public void onComponentTag(Component component, ComponentTag tag) { super.onComponentTag(component, tag); String tagName = tag.getName().toLowerCase(Locale.ROOT); boolean hasLengthAttribute = hasLengthAttribute(tagName); Integer maximum = getMaximum(); if (maximum != null && hasLengthAttribute) { tag.put("maxlength", maximum); } Integer minimum = getMinimum(); if (minimum != null && hasLengthAttribute) { tag.put("minlength", minimum); } }
@Override public void validate(IValidatable<V> validatable) { R value = getValue(validatable); final R min = getMinimum(); final R max = getMaximum(); if ((min != null && value.compareTo(min) < 0) || (max != null && value.compareTo(max) > 0)) { Mode mode = getMode(); ValidationError error = new ValidationError(this, mode.getVariation()); if (min != null) { error.setVariable("minimum", min); } if (max != null) { error.setVariable("maximum", max); } if (mode == Mode.EXACT) { error.setVariable("exact", max); } validatable.error(decorate(error, validatable)); } }
@Override protected IValidationError decorate(IValidationError error, IValidatable<String> validatable) { error = super.decorate(error, validatable); if (error instanceof ValidationError) { ((ValidationError)error).setVariable("length", validatable.getValue().length()); } return error; }
@Override public void onComponentTag(Component component, ComponentTag tag) { super.onComponentTag(component, tag); String tagName = tag.getName().toLowerCase(Locale.ROOT); boolean hasLengthAttribute = hasLengthAttribute(tagName); Integer maximum = getMaximum(); if (maximum != null && hasLengthAttribute) { tag.put("maxlength", maximum); } Integer minimum = getMinimum(); if (minimum != null && hasLengthAttribute) { tag.put("minlength", minimum); } }
/** * Gets validation mode which is determined by whether min, max, or both values are provided * * @return validation mode */ public final Mode getMode() { final R min = getMinimum(); final R max = getMaximum(); if (min == null && max != null) { return Mode.MAXIMUM; } else if (max == null && min != null) { return Mode.MINIMUM; } else if ((min == null && max == null) || max.equals(min)) { return Mode.EXACT; } else { return Mode.RANGE; } }
/** * Constructor that sets the minimum and maximum values. * * @param minimum * the minimum value * @param maximum * the maximum value */ public AbstractRangeValidator(R minimum, R maximum) { setRange(minimum, maximum); }