com.vaadin.data.validator.StringLengthValidator Maven / Gradle / Ivy
/*
* Vaadin Framework 7
*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See for the full
* license.
*/
package com.vaadin.data.validator;
/**
* This StringLengthValidator
is used to validate the length of
* strings.
*
* @author Vaadin Ltd.
* @since 3.0
*/
@SuppressWarnings("serial")
public class StringLengthValidator extends AbstractStringValidator {
private Integer minLength = null;
private Integer maxLength = null;
private boolean allowNull = true;
/**
* Creates a new StringLengthValidator with a given error message.
*
* @param errorMessage
* the message to display in case the value does not validate.
*/
public StringLengthValidator(String errorMessage) {
super(errorMessage);
}
/**
* Creates a new StringLengthValidator with a given error message and
* minimum and maximum length limits.
*
* @param errorMessage
* the message to display in case the value does not validate.
* @param minLength
* the minimum permissible length of the string or null for no
* limit. A negative value for no limit is also supported for
* backwards compatibility.
* @param maxLength
* the maximum permissible length of the string or null for no
* limit. A negative value for no limit is also supported for
* backwards compatibility.
* @param allowNull
* Are null strings permissible? This can be handled better by
* setting a field as required or not.
*/
public StringLengthValidator(String errorMessage, Integer minLength,
Integer maxLength, boolean allowNull) {
this(errorMessage);
setMinLength(minLength);
setMaxLength(maxLength);
setNullAllowed(allowNull);
}
/**
* Checks if the given value is valid.
*
* @param value
* the value to validate.
* @return true
for valid value, otherwise false
.
*/
@Override
protected boolean isValidValue(String value) {
if (value == null) {
return allowNull;
}
final int len = value.length();
if ((minLength != null && minLength > -1 && len < minLength)
|| (maxLength != null && maxLength > -1 && len > maxLength)) {
return false;
}
return true;
}
/**
* Returns true
if null strings are allowed.
*
* @return true
if allows null string, otherwise
* false
.
*/
@Deprecated
public final boolean isNullAllowed() {
return allowNull;
}
/**
* Gets the maximum permissible length of the string.
*
* @return the maximum length of the string or null if there is no limit
*/
public Integer getMaxLength() {
return maxLength;
}
/**
* Gets the minimum permissible length of the string.
*
* @return the minimum length of the string or null if there is no limit
*/
public Integer getMinLength() {
return minLength;
}
/**
* Sets whether null-strings are to be allowed. This can be better handled
* by setting a field as required or not.
*/
@Deprecated
public void setNullAllowed(boolean allowNull) {
this.allowNull = allowNull;
}
/**
* Sets the maximum permissible length of the string.
*
* @param maxLength
* the maximum length to accept or null for no limit
*/
public void setMaxLength(Integer maxLength) {
this.maxLength = maxLength;
}
/**
* Sets the minimum permissible length.
*
* @param minLength
* the minimum length to accept or null for no limit
*/
public void setMinLength(Integer minLength) {
this.minLength = minLength;
}
}