use of com.vaadin.flow.data.binder.ValueContext in project furms by unity-idm.
the class AlarmFormView method prepareValidator.
private void prepareValidator(TextField nameField, ComboBox<String> allocationComboBox, IntegerField thresholdField, Checkbox checkbox, MultiselectComboBox<String> multiselectComboBox) {
binder.forField(nameField).withValidator(value -> Objects.nonNull(value) && !value.isBlank(), getTranslation("view.project-admin.alarms.form.error.name")).bind(model -> model.name, (model, name) -> model.name = name);
binder.forField(allocationComboBox).withValidator(Objects::nonNull, getTranslation("view.project-admin.alarms.form.error.allocation")).bind(model -> model.allocationId, (model, id) -> model.allocationId = id);
binder.forField(thresholdField).withValidator(threshold -> threshold >= 1.0 && threshold <= 100.0, getTranslation("view.project-admin.alarms.form.error.threshold")).bind(model -> model.threshold, (model, threshold) -> model.threshold = threshold);
binder.forField(checkbox).bind(model -> model.allUsers, (model, value) -> model.allUsers = value);
binder.forField(multiselectComboBox).withValidator(emails -> emails.stream().noneMatch(email -> emailValidator.apply(email, new ValueContext()).isError()), getTranslation("view.project-admin.alarms.form.error.emails")).bind(model -> model.users, (model, policyFile) -> model.users = multiselectComboBox.getSelectedItems());
}
use of com.vaadin.flow.data.binder.ValueContext in project flow by vaadin.
the class NotEmptyValidatorTest method nonNullValueIsAllowed.
@Test
public void nonNullValueIsAllowed() {
NotEmptyValidator<Object> validator = new NotEmptyValidator<>("foo");
Object value = new Object();
ValidationResult result = validator.apply(value, new ValueContext());
Assert.assertFalse(result.isError());
Assert.assertFalse(result.isError());
}
use of com.vaadin.flow.data.binder.ValueContext in project flow by vaadin.
the class NotEmptyValidatorTest method emptyValueIsDisallowed.
@Test
public void emptyValueIsDisallowed() {
NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo");
ValidationResult result = validator.apply("", new ValueContext());
Assert.assertTrue(result.isError());
Assert.assertEquals("foo", result.getErrorMessage());
}
use of com.vaadin.flow.data.binder.ValueContext in project flow by vaadin.
the class ValidatorTestBase method assertFails.
protected <T> void assertFails(T value, String errorMessage, Validator<? super T> validator) {
ValidationResult result = validator.apply(value, new ValueContext(localeContext));
Assert.assertTrue(result.isError());
Assert.assertEquals(errorMessage, result.getErrorMessage());
}
use of com.vaadin.flow.data.binder.ValueContext in project flow by vaadin.
the class BeanValidator method apply.
/**
* Validates the given value as if it were the value of the bean property
* configured for this validator. Returns {@code Result.ok} if there are no
* JSR-303 constraint violations, a {@code Result.error} of chained
* constraint violation messages otherwise.
* <p>
* Null values are accepted unless the property has an {@code @NotNull}
* annotation or equivalent.
*
* @param value
* the input value to validate
* @param context
* the value context for validation
* @return the validation result
*/
@Override
public ValidationResult apply(final Object value, ValueContext context) {
Set<? extends ConstraintViolation<?>> violations = getJavaxBeanValidator().validateValue(beanType, propertyName, value);
Locale locale = context.getLocale().orElse(Locale.getDefault());
Optional<ValidationResult> result = violations.stream().map(violation -> ValidationResult.error(getMessage(violation, locale))).findFirst();
return result.orElse(ValidationResult.ok());
}
Aggregations