Search in sources :

Example 1 with Label

use of com.vaadin.flow.component.html.Label in project flow by vaadin.

the class BinderConverterValidatorTest method binderBindAndLoad_clearsErrors.

@Test
public void binderBindAndLoad_clearsErrors() {
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(notEmpty);
    binding.bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(bean -> !bean.getFirstName().contains("error"), "error");
    Person person = new Person();
    person.setFirstName("");
    binder.setBean(person);
    // initial value is invalid but no error
    Assert.assertFalse(componentErrors.containsKey(nameField));
    // make error show
    nameField.setValue("foo");
    nameField.setValue("");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    // bind to another person to see that error is cleared
    person = new Person();
    person.setFirstName("");
    binder.setBean(person);
    // error has been cleared
    Assert.assertFalse(componentErrors.containsKey(nameField));
    // make show error
    nameField.setValue("foo");
    nameField.setValue("");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    // load should also clear error
    binder.readBean(person);
    Assert.assertFalse(componentErrors.containsKey(nameField));
    // bind a new field that has invalid value in bean
    TestTextField lastNameField = new TestTextField();
    // The test starts with a valid value as the last name of the person,
    // since the binder assumes any non-changed values to be valid.
    person.setLastName("bar");
    BindingBuilder<Person, String> binding2 = binder.forField(lastNameField).withValidator(notEmpty);
    binding2.bind(Person::getLastName, Person::setLastName);
    // should not have error shown when initialized
    assertThat(lastNameField.getErrorMessage(), isEmptyString());
    // Set a value that breaks the validation
    lastNameField.setValue("");
    assertNotNull(lastNameField.getErrorMessage());
    // add status label to show bean level error
    Label statusLabel = new Label();
    binder.setStatusLabel(statusLabel);
    nameField.setValue("error");
    // no error shown yet because second field validation doesn't pass
    Assert.assertEquals("", statusLabel.getText());
    // make second field validation pass to get bean validation error
    lastNameField.setValue("foo");
    Assert.assertEquals("error", statusLabel.getText());
    // reload bean to clear error
    binder.readBean(person);
    Assert.assertEquals("", statusLabel.getText());
    // reset() should clear all errors and status label
    nameField.setValue("");
    lastNameField.setValue("");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    Assert.assertTrue(componentErrors.containsKey(lastNameField));
    binder.removeBean();
    Assert.assertFalse(componentErrors.containsKey(nameField));
    Assert.assertFalse(componentErrors.containsKey(lastNameField));
    Assert.assertEquals("", statusLabel.getText());
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Label(com.vaadin.flow.component.html.Label) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 2 with Label

use of com.vaadin.flow.component.html.Label in project flow by vaadin.

the class BinderValidationStatusTest method binderWithStatusLabel_addAfterBound.

@Test(expected = IllegalStateException.class)
public void binderWithStatusLabel_addAfterBound() {
    Label label = new Label();
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(notEmpty);
    binding.bind(Person::getFirstName, Person::setFirstName);
    binding.withStatusLabel(label);
}
Also used : Label(com.vaadin.flow.component.html.Label) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 3 with Label

use of com.vaadin.flow.component.html.Label in project flow by vaadin.

the class BinderValidationStatusTest method bindingWithStatusLabel_addAfterBound.

@Test(expected = IllegalStateException.class)
public void bindingWithStatusLabel_addAfterBound() {
    Label label = new Label();
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(notEmpty);
    binding.bind(Person::getFirstName, Person::setFirstName);
    binding.withStatusLabel(label);
}
Also used : Label(com.vaadin.flow.component.html.Label) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 4 with Label

use of com.vaadin.flow.component.html.Label in project flow by vaadin.

the class BinderValidationStatusTest method bindingWithStatusHandler_setAfterLabel.

@Test(expected = IllegalStateException.class)
public void bindingWithStatusHandler_setAfterLabel() {
    Label label = new Label();
    BindingBuilder<Person, String> binding = binder.forField(nameField);
    binding.withStatusLabel(label);
    binding.withValidationStatusHandler(NOOP);
}
Also used : Label(com.vaadin.flow.component.html.Label) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 5 with Label

use of com.vaadin.flow.component.html.Label in project flow by vaadin.

the class BinderValidationStatusTest method binderWithStatusHandler_setAfterLabel.

@Test(expected = IllegalStateException.class)
public void binderWithStatusHandler_setAfterLabel() {
    Label label = new Label();
    BindingBuilder<Person, String> binding = binder.forField(nameField);
    binding.bind(Person::getFirstName, Person::setFirstName);
    binder.setStatusLabel(label);
    binder.setValidationStatusHandler(event -> {
    });
}
Also used : Label(com.vaadin.flow.component.html.Label) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Aggregations

Label (com.vaadin.flow.component.html.Label)17 Test (org.junit.Test)10 Person (com.vaadin.flow.tests.data.bean.Person)9 NativeButton (com.vaadin.flow.component.html.NativeButton)3 UI (com.vaadin.flow.component.UI)1 Div (com.vaadin.flow.component.html.Div)1 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1 Before (org.junit.Before)1