/** * 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 input24() { // tag::input24[] Input<String> input = Input.string().build(); ValidatableInput<String> validatableInput = ValidatableInput.builder(input) // <1> .withValidator(Validator.max(10)) // <2> .build(); validatableInput = ValidatableInput.from(input); // <3> validatableInput.addValidator(Validator.max(10)); // <4> // end::input24[] }
/** * Build a validator that checks that given value is lower than or equal to <code>max</code> value, and uses given * {@link Localizable} <code>message</code> 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 * @param message Validation error message * @return Validator * @throws UnsupportedValidationTypeException If value to validate is of an unsupported data type */ static <T> Validator<T> max(double max, Localizable message) { ObjectUtils.argumentNotNull(message, "Validation error message must be not null"); return max(max, message.getMessage(), message.getMessageCode()); }
public void input34() { // tag::input34[] PropertyInputForm.formLayout(SUBJECT) // <1> .withValidator(NAME, Validator.max(10)) // <2> .withValidator(propertyBox -> { // <3> /* group value validation */ }).validateOnValueChange(true) // <4> .build(); // end::input34[] }
public void input23() { // tag::input23[] ValidatableInput<String> validatableInput = Input.string() // .validatable() // <1> .withValidator(Validator.max(10)) // <2> .validateOnValueChange(true) // <3> .build(); // end::input23[] }
public void listing26() { // tag::listing26[] final StringProperty NAME = StringProperty.create("name").withValidator(Validator.notBlank()); // <1> PropertyListing.builder(SUBJECT) // .withValidator(NAME, Validator.max(10)) // <2> .build(); // end::listing26[] }
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 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 input6() { // tag::input6[] ValidatableInput<String> validatableInput = ValidatableInput.from(Components.input.string().build()); // <1> validatableInput = ValidatableInput.from(new TextField()); // <2> validatableInput = Components.input.string().validatable() // <3> .required("Value is required") // <4> .withValidator(Validator.max(100)).build(); // <5> // end::input6[] }
public void listing12() { // tag::listing12[] PropertyListing listing = Components.listing.properties(PROPERTIES) // .editable(true) // <1> .editorSaveCaption("Save item") // <2> .editorCancelCaption("Discard") // <3> .editable(ID, false) // <4> .editor(DESCRIPTION, new TextField()) // <5> .withValidator(DESCRIPTION, Validator.max(100)) // <6> .required(ID) // <7> .build(); // end::listing12[] }
public void validatableBuilders() { // tag::validatable1[] Input<String> stringInput = Components.input.string().build(); ValidatableInput<String> validatableInput = ValidatableInput.from(stringInput); // <1> validatableInput = ValidatableInput.from(new TextField()); // <2> // end::validatable1[] // tag::validatable2[] validatableInput = Components.input.string() // .caption("The caption") // .validatable() // <1> .required("Value is required") // <2> .withValidator(Validator.max(100)) // <3> .build(); // end::validatable2[] }
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[] }
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[] }
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(), getValidationMessage(property, a.message(), Validator.ValidationMessage.MIN))); property.validator(Validator.max(Integer.valueOf(a.max()).doubleValue(), getValidationMessage(property, a.message(), Validator.ValidationMessage.MAX))); LOGGER.debug(() -> "BeanPropertyBeanValidationPostProcessor: added validator to property ["
public void listing12() { // tag::listing12[] PropertyListing listing = Components.listing.properties(PROPERTIES) // .editable(true) // <1> .editorBuffered(true) // <2> .editorSaveCaption("Save item") // <3> .editorCancelCaption("Discard") // <4> .editable(ID, false) // <5> .editor(DESCRIPTION, new TextField()) // <6> .withValidator(Validator.create(pb -> pb.getValue(DESCRIPTION) != null, "Description must be not null")) // <7> .withValidator(DESCRIPTION, Validator.max(100)) // <8> .required(ID) // <9> .build(); // end::listing12[] }