/** * Builds a validator out of a conditional function and an error message * provider. If the function returns true, the validator returns * {@code Result.ok()}; if it returns false or throws an exception, * {@code Result.error()} is returned with the message from the provider. * * @param <T> * the value type * @param guard * the function used to validate, not null * @param errorMessageProvider * the provider to generate error messages, not null * @return the new validator using the function */ public static <T> Validator<T> from(SerializablePredicate<T> guard, ErrorMessageProvider errorMessageProvider) { return from(guard, errorMessageProvider, ErrorLevel.ERROR); }
/** * Builds a validator out of a conditional function and an error message. If * the function returns true, the validator returns {@code Result.ok()}; if * it returns false or throws an exception, * {@link ValidationResult#error(String)} is returned with the given message * and error level {@link ErrorLevel#ERROR}. * <p> * For instance, the following validator checks if a number is between 0 and * 10, inclusive: * * <pre> * Validator<Integer> v = Validator.from(num -> num >= 0 && num <= 10, * "number must be between 0 and 10"); * </pre> * * @param <T> * the value type * @param guard * the function used to validate, not null * @param errorMessage * the message returned if validation fails, not null * @return the new validator using the function */ public static <T> Validator<T> from(SerializablePredicate<T> guard, String errorMessage) { Objects.requireNonNull(errorMessage, "errorMessage cannot be null"); return from(guard, ctx -> errorMessage); }
String errorMessage, ErrorLevel errorLevel) { Objects.requireNonNull(errorMessage, "errorMessage cannot be null"); return from(guard, ctx -> errorMessage, errorLevel);
@Override public BindingBuilder<BEAN, TARGET> asRequired( ErrorMessageProvider errorMessageProvider) { return asRequired(Validator.from( value -> !Objects.equals(value, field.getEmptyValue()), errorMessageProvider)); }
/** * A convenience method to add a validator to this binder using the * {@link Validator#from(SerializablePredicate, String)} factory method. * <p> * Bean level validators are applied on the bean instance after the bean is * updated. If the validators fail, the bean instance is reverted to its * previous state. * * @see #writeBean(Object) * @see #writeBeanIfValid(Object) * @see #withValidator(Validator) * @see #withValidator(SerializablePredicate, ErrorMessageProvider) * * @param predicate * the predicate performing validation, not null * @param message * the error message to report in case validation failure * @return this binder, for chaining */ public Binder<BEAN> withValidator(SerializablePredicate<BEAN> predicate, String message) { return withValidator(Validator.from(predicate, message)); }
/** * A convenience method to add a validator to this binding using the * {@link Validator#from(SerializablePredicate, ErrorMessageProvider)} * factory method. * <p> * Validators are applied, in registration order, when the field value * is written to the backing property. If any validator returns a * failure, the property value is not updated. * * @see #withValidator(Validator) * @see #withValidator(SerializablePredicate, String) * @see #withValidator(SerializablePredicate, ErrorMessageProvider, * ErrorLevel) * @see Validator#from(SerializablePredicate, ErrorMessageProvider) * * @param predicate * the predicate performing validation, not null * @param errorMessageProvider * the provider to generate error messages, not null * @return this binding, for chaining * @throws IllegalStateException * if {@code bind} has already been called */ public default BindingBuilder<BEAN, TARGET> withValidator( SerializablePredicate<? super TARGET> predicate, ErrorMessageProvider errorMessageProvider) { return withValidator( Validator.from(predicate, errorMessageProvider)); }
/** * A convenience method to add a validator to this binder using the * {@link Validator#from(SerializablePredicate, ErrorMessageProvider)} * factory method. * <p> * Bean level validators are applied on the bean instance after the bean is * updated. If the validators fail, the bean instance is reverted to its * previous state. * * @see #writeBean(Object) * @see #writeBeanIfValid(Object) * @see #withValidator(Validator) * @see #withValidator(SerializablePredicate, String) * * @param predicate * the predicate performing validation, not null * @param errorMessageProvider * the provider to generate error messages, not null * @return this binder, for chaining */ public Binder<BEAN> withValidator(SerializablePredicate<BEAN> predicate, ErrorMessageProvider errorMessageProvider) { return withValidator(Validator.from(predicate, errorMessageProvider)); }
ErrorLevel errorLevel) { return withValidator( Validator.from(predicate, message, errorLevel));
/** * A convenience method to add a validator to this binding using the * {@link Validator#from(SerializablePredicate, String)} factory method. * <p> * Validators are applied, in registration order, when the field value * is written to the backing property. If any validator returns a * failure, the property value is not updated. * * @see #withValidator(Validator) * @see #withValidator(SerializablePredicate, String, ErrorLevel) * @see #withValidator(SerializablePredicate, ErrorMessageProvider) * @see Validator#from(SerializablePredicate, String) * * @param predicate * the predicate performing validation, not null * @param message * the error message to report in case validation failure * @return this binding, for chaining * @throws IllegalStateException * if {@code bind} has already been called */ public default BindingBuilder<BEAN, TARGET> withValidator( SerializablePredicate<? super TARGET> predicate, String message) { return withValidator(Validator.from(predicate, message)); }
ErrorMessageProvider errorMessageProvider, ErrorLevel errorLevel) { return withValidator(Validator.from(predicate, errorMessageProvider, errorLevel));
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[] }