Search in sources :

Example 1 with ValueContext

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());
}
Also used : DuplicatedNameValidationError(io.imunity.furms.api.validation.exceptions.DuplicatedNameValidationError) Label(com.vaadin.flow.component.html.Label) PageTitle(io.imunity.furms.ui.components.PageTitle) BeforeEvent(com.vaadin.flow.router.BeforeEvent) FurmsFormLayout(io.imunity.furms.ui.components.FurmsFormLayout) Route(com.vaadin.flow.router.Route) Map(java.util.Map) Key(com.vaadin.flow.component.Key) UI(com.vaadin.flow.component.UI) TextField(com.vaadin.flow.component.textfield.TextField) FormButtons(io.imunity.furms.ui.components.FormButtons) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) BreadCrumbParameter(io.imunity.furms.ui.components.layout.BreadCrumbParameter) FormLayout(com.vaadin.flow.component.formlayout.FormLayout) OptionalParameter(com.vaadin.flow.router.OptionalParameter) Objects(java.util.Objects) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) EmailNotPresentException(io.imunity.furms.api.validation.exceptions.EmailNotPresentException) AlarmAlreadyExceedThresholdException(io.imunity.furms.api.validation.exceptions.AlarmAlreadyExceedThresholdException) AlarmService(io.imunity.furms.api.alarms.AlarmService) Optional(java.util.Optional) VaadinExceptionHandler.handleExceptions(io.imunity.furms.ui.utils.VaadinExceptionHandler.handleExceptions) ResourceGetter.getCurrentResourceId(io.imunity.furms.ui.utils.ResourceGetter.getCurrentResourceId) NotificationUtils.showErrorNotification(io.imunity.furms.ui.utils.NotificationUtils.showErrorNotification) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Binder(com.vaadin.flow.data.binder.Binder) Div(com.vaadin.flow.component.html.Div) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Function(java.util.function.Function) HashSet(java.util.HashSet) AlarmWithUserEmails(io.imunity.furms.domain.alarms.AlarmWithUserEmails) TextFieldVariant(com.vaadin.flow.component.textfield.TextFieldVariant) MultiselectComboBox(org.vaadin.gatanaso.MultiselectComboBox) ProjectAllocationService(io.imunity.furms.api.project_allocation.ProjectAllocationService) FurmsViewComponent(io.imunity.furms.ui.components.FurmsViewComponent) IntegerField(com.vaadin.flow.component.textfield.IntegerField) BeanValidationBinder(com.vaadin.flow.data.binder.BeanValidationBinder) ValueContext(com.vaadin.flow.data.binder.ValueContext) ProjectAllocation(io.imunity.furms.domain.project_allocation.ProjectAllocation) EAGER(com.vaadin.flow.data.value.ValueChangeMode.EAGER) ProjectAdminMenu(io.imunity.furms.ui.views.project.ProjectAdminMenu) FiredAlarmThresholdReduceException(io.imunity.furms.api.validation.exceptions.FiredAlarmThresholdReduceException) ButtonVariant(com.vaadin.flow.component.button.ButtonVariant) Optional.ofNullable(java.util.Optional.ofNullable) AlarmId(io.imunity.furms.domain.alarms.AlarmId) Button(com.vaadin.flow.component.button.Button) ProjectService(io.imunity.furms.api.projects.ProjectService) ValueContext(com.vaadin.flow.data.binder.ValueContext)

Example 2 with ValueContext

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());
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) ValidationResult(com.vaadin.flow.data.binder.ValidationResult) Test(org.junit.Test)

Example 3 with ValueContext

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());
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) ValidationResult(com.vaadin.flow.data.binder.ValidationResult) Test(org.junit.Test)

Example 4 with ValueContext

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());
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) ValidationResult(com.vaadin.flow.data.binder.ValidationResult)

Example 5 with ValueContext

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());
}
Also used : Locale(java.util.Locale) Validator(com.vaadin.flow.data.binder.Validator) ConstraintDescriptor(javax.validation.metadata.ConstraintDescriptor) Context(javax.validation.MessageInterpolator.Context) Set(java.util.Set) ValidatorFactory(javax.validation.ValidatorFactory) Serializable(java.io.Serializable) Objects(java.util.Objects) ValidationResult(com.vaadin.flow.data.binder.ValidationResult) Locale(java.util.Locale) Optional(java.util.Optional) Validation(javax.validation.Validation) ValueContext(com.vaadin.flow.data.binder.ValueContext) BeanUtil(com.vaadin.flow.internal.BeanUtil) ConstraintViolation(javax.validation.ConstraintViolation) ValidationResult(com.vaadin.flow.data.binder.ValidationResult)

Aggregations

ValueContext (com.vaadin.flow.data.binder.ValueContext)31 Test (org.junit.jupiter.api.Test)24 Locale (java.util.Locale)6 ValidationResult (com.vaadin.flow.data.binder.ValidationResult)5 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)5 LocalDate (java.time.LocalDate)4 LocalDateTime (java.time.LocalDateTime)4 Matchers.is (org.hamcrest.Matchers.is)4 Matchers.nullValue (org.hamcrest.Matchers.nullValue)4 Test (org.junit.Test)3 Result (com.vaadin.flow.data.binder.Result)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Key (com.vaadin.flow.component.Key)1 UI (com.vaadin.flow.component.UI)1 Button (com.vaadin.flow.component.button.Button)1 ButtonVariant (com.vaadin.flow.component.button.ButtonVariant)1 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)1