/** * Checks the validity of the given <code>value</code> against every registered validator, if any. If the value * is not valid, an {@link ValidationException} is thrown. * <p> * The {@link ValidationException} is {@link Localizable}, providing optional message code and arguments for * validation message localization. * </p> * @param value Value to validate * @throws ValidationException If the value is not valid, providing the validation error message. */ default void validate(T value) throws ValidationException { LinkedList<ValidationException> failures = new LinkedList<>(); for (Validator<T> validator : getValidators()) { try { validator.validate(value); } catch (ValidationException ve) { failures.add(ve); } } if (!failures.isEmpty()) { throw (failures.size() == 1) ? failures.getFirst() : new ValidationException(failures.toArray(new ValidationException[failures.size()])); } }
public void validator() { // tag::validator[] Validator<String> validator = v -> { // <1> if (v.length() < 10) throw new ValidationException("Value must be at least 10 characters"); }; validator = Validator.create(v -> v.length() >= 10, "Value must be at least 10 characters"); // <2> validator = Validator.create(v -> v.length() >= 10, "Value must be at least 10 characters", "messageLocalizationCode"); // <3> // end::validator[] }
/** * Build a validator that checks that given value is lower than or equal to <code>max</code> value, and uses * {@link ValidationMessage#MAX} as validation error message. * <p> * Supported data types: {@link CharSequence}, {@link Number}, {@link Collection}, {@link Map} and arrays. * </p> * <ul> * <li>String: the string length is checked against given max value (converted to a long)</li> * <li>Integer numbers: the number value is checked against given max value (converted to a long)</li> * <li>Decimal numbers: the number value is checked against given max value</li> * <li>Collections, Maps and arrays: size/length is checked against given max value</li> * </ul> * <p> * If the data type only supports integer max value validation (for example, a String length), the given * <code>max</code> value is treated as an integer value, casting it to an <code>int</code> or a <code>long</code>. * </p> * @param <T> Validator type * @param max Max value * @return Validator * @throws UnsupportedValidationTypeException If value to validate is of an unsupported data type */ static <T> Validator<T> max(double max) { return max(max, ValidationMessage.MAX); }
public void bultin() { // tag::builtin[] try { Validator.notEmpty().validate("mustBeNotEmpty"); // <1> Validator.notEmpty("Value must be not empty", "myLocalizationMessageCode").validate("mustBeNotEmpty"); // <2> } catch (ValidationException e) { // invalid value System.out.println(e.getLocalizedMessage()); // <3> } // end::builtin[] }
.isNull(getValidationMessage(property, a.message(), Validator.ValidationMessage.NULL))); LOGGER.debug(() -> "BeanPropertyBeanValidationPostProcessor: added validator to property [" + property + "] for constraint [" + Null.class.getName() + "]"); property.validator(Validator.notNull( getValidationMessage(property, a.message(), Validator.ValidationMessage.NOT_NULL))); LOGGER.debug(() -> "BeanPropertyBeanValidationPostProcessor: added validator to property [" property.validator(Validator.min(Long.valueOf(a.value()).doubleValue(), getValidationMessage(property, a.message(), Validator.ValidationMessage.MIN))); LOGGER.debug( }); property.getAnnotation(DecimalMin.class).ifPresent(a -> { property.validator(Validator.min(new BigDecimal(a.value()).doubleValue(), getValidationMessage(property, a.message(), Validator.ValidationMessage.MIN))); LOGGER.debug(() -> "BeanPropertyBeanValidationPostProcessor: added validator to property [" property.validator(Validator.max(Long.valueOf(a.value()).doubleValue(), getValidationMessage(property, a.message(), Validator.ValidationMessage.MAX))); LOGGER.debug( }); property.getAnnotation(DecimalMax.class).ifPresent(a -> { property.validator(Validator.max(new BigDecimal(a.value()).doubleValue(), getValidationMessage(property, a.message(), Validator.ValidationMessage.MAX))); LOGGER.debug(() -> "BeanPropertyBeanValidationPostProcessor: added validator to property [" property.validator(Validator.min(Integer.valueOf(a.min()).doubleValue(),
public void group4a() { // tag::group4a[] PropertyInputGroup group = Components.input.propertyGroup().properties(PROPERTIES) // .withValidator(DESCRIPTION, Validator.max(100)) // <1> .withValidator(ID, com.vaadin.data.Validator.from(id -> id != null, "Id must be not null")) // <2> // group validation .withValidator(Validator.create(propertyBox -> propertyBox.getValue(ID) > 0, "The ID value must be greater than 0")) // <3> .build(); // end::group4a[] }
public void validatable() { // tag::validatable3[] ValidatableInput<String> validatableInput = Components.input.string().validatable().build(); // <1> validatableInput.addValidator(Validator.max(100)); // <2> validatableInput.addValidator(Validator.email("Must be a valid e-mail address", "invalid.email.message.code")); // <3> validatableInput .addValidator(Validator.create(value -> value.length() >= 3, "Must be at least 3 characters long")); // <4> // end::validatable3[] // tag::validatable4[] try { validatableInput.validate(); // <1> } catch (ValidationException e) { // do something at validation failure } boolean valid = validatableInput.isValid(); // <2> // end::validatable4[] }
public void input5() { // tag::input5[] Input<String> stringInput = Components.input.string().build(); ValidatableInput<String> validatableInput = ValidatableInput.from(stringInput); // <1> validatableInput.addValidator(Validator.email()); // <2> validatableInput.addValidator(Validator.max(100)); // <3> validatableInput.setValidationStatusHandler(e -> { // <4> if (e.isInvalid()) { Notification.show(e.getErrorMessage(), Type.ERROR_MESSAGE); } }); validatableInput.validate(); // <5> validatableInput.setValidateOnValueChange(true); // <6> // end::input5[] }
public void propertyBox2() { // tag::propertybox2[] final PathProperty<Long> ID = PathProperty.create("id", Long.class).withValidator(Validator.notNull()); // <1> final StringProperty NAME = StringProperty.create("name").withValidator(Validator.notBlank()); // <2> final PropertySet<?> PROPERTIES = PropertySet.of(ID, NAME); PropertyBox propertyBox = PropertyBox.create(PROPERTIES); propertyBox.setValue(ID, null); // <3> propertyBox = PropertyBox.builder(PROPERTIES).invalidAllowed(true).build(); // <4> propertyBox.validate(); // <5> // end::propertybox2[] }
/** * Build a validator that checks that the value is a valid e-mail address using RFC822 format rules, and uses * default {@link ValidationMessage#EMAIL} message as validation error message. * <p> * Supported data types: {@link CharSequence} * </p> * @param <T> Value and validator type * @return Validator */ static <T extends CharSequence> Validator<T> email() { return email(ValidationMessage.EMAIL); }
/** * Build a validator that checks that given value is not <code>null</code> nor empty, trimming spaces, and uses * default {@link ValidationMessage#NOT_EMPTY} localizable message as validation error message. * <p> * Supported data types: {@link CharSequence} * </p> * @param <T> Validator type * @return Validator */ static <T extends CharSequence> Validator<T> notBlank() { return notBlank(ValidationMessage.NOT_EMPTY); }
/** * Build a validator that checks that given value is not <code>null</code> nor empty, and uses default * {@link ValidationMessage#NOT_EMPTY} localizable message as validation error message. * <p> * Supported data types: {@link CharSequence}, {@link Collection}, {@link Map} and Arrays * </p> * @param <T> Validator type * @return Validator * @throws UnsupportedValidationTypeException If value to validate is of an unsupported data type */ static <T> Validator<T> notEmpty() { return notEmpty(ValidationMessage.NOT_EMPTY); }
/** * Build a validator that checks that given value is not <code>null</code> and uses default * {@link ValidationMessage#NOT_NULL} localizable message as validation error message. * <p> * Supported data types: all * </p> * @param <T> Validator type * @return Validator */ static <T> Validator<T> notNull() { return notNull(ValidationMessage.NOT_NULL); }
/** * Build a validator that checks that a value is less than given <code>compareTo</code> value, and uses default * {@link ValidationMessage#LESS_THAN} message as validation error message. * <p> * Supported data types: {@link Comparable} * </p> * @param <T> Value and validator type * @param compareTo Value to compare * @return Validator */ static <T extends Comparable<T>> Validator<T> lessThan(T compareTo) { return lessThan(compareTo, ValidationMessage.LESS_THAN); }
public void group4() { // tag::group4[] final PathProperty<Long> ID = PathProperty.create("id", Long.class); final PathProperty<String> DESCRIPTION = PathProperty.create("description", String.class); final PropertySet<?> PROPERTIES = PropertySet.of(ID, DESCRIPTION); PropertyInputGroup group = Components.input.propertyGroup().properties(PROPERTIES) // .withValidator(DESCRIPTION, Validator.max(100)) // <1> .required(ID) // <2> .required(ID, "The ID value is required") // <3> .withValidator(Validator.create(propertyBox -> propertyBox.getValue(ID) > 0, "The ID value must be greater than 0")) // <4> .build(); // end::group4[] }
/** * Build a validator that checks that the value is a valid e-mail address using RFC822 format rules, and uses given * {@link Localizable} message as validation error message. * <p> * Supported data types: {@link CharSequence} * </p> * @param <T> Value and validator type * @param message Validation error message * @return Validator */ static <T extends CharSequence> Validator<T> email(Localizable message) { ObjectUtils.argumentNotNull(message, "Validation error message must be not null"); return email(message.getMessage(), message.getMessageCode()); }
/** * Build a validator that checks that given value is not <code>null</code> nor empty, trimming spaces, and uses * given {@link Localizable} <code>message</code> as validation error message. * <p> * Supported data types: {@link CharSequence} * </p> * @param <T> Validator type * @param message Validation error message * @return Validator */ static <T extends CharSequence> Validator<T> notBlank(Localizable message) { ObjectUtils.argumentNotNull(message, "Validation error message must be not null"); return notBlank(message.getMessage(), message.getMessageCode()); }
/** * Build a validator that checks that given value is not <code>null</code> nor empty, and uses given * {@link Localizable} <code>message</code> as validation error message. * <p> * Supported data types: {@link CharSequence}, {@link Collection}, {@link Map} and Arrays * </p> * @param <T> Validator type * @param message Validation error message * @return Validator * @throws UnsupportedValidationTypeException If value to validate is of an unsupported data type */ static <T> Validator<T> notEmpty(Localizable message) { ObjectUtils.argumentNotNull(message, "Validation error message must be not null"); return notEmpty(message.getMessage(), message.getMessageCode()); }