Search in sources :

Example 1 with HasValue

use of com.vaadin.flow.component.HasValue in project flow by vaadin.

the class BinderTest method setValidationErrorHandler_handlerIsSet_handlerMethodsAreCalled.

@Test
public void setValidationErrorHandler_handlerIsSet_handlerMethodsAreCalled() {
    TestTextField testField = new TestTextField();
    class TestErrorHandler implements BinderValidationErrorHandler {

        private ValidationResult result;

        private boolean clearIsCalled;

        @Override
        public void handleError(HasValue<?, ?> field, ValidationResult result) {
            Assert.assertSame(testField, field);
            this.result = result;
            clearIsCalled = false;
        }

        @Override
        public void clearError(HasValue<?, ?> field) {
            Assert.assertSame(testField, field);
            result = null;
            clearIsCalled = true;
        }
    }
    ;
    TestErrorHandler handler = new TestErrorHandler();
    binder.setValidationErrorHandler(handler);
    binder.forField(testField).asRequired().withValidator((val, context) -> {
        if ("bar".equals(val)) {
            return ValidationResult.error("foo");
        }
        return ValidationResult.ok();
    }).bind(Person::getFirstName, Person::setFirstName);
    binder.setBean(new Person());
    testField.setValue("bar");
    Assert.assertTrue(handler.result.isError());
    Assert.assertFalse(handler.clearIsCalled);
    testField.setValue("foo");
    Assert.assertNull(handler.result);
    Assert.assertTrue(handler.clearIsCalled);
    Assert.assertSame(handler, binder.getValidationErrorHandler());
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) HasValue(com.vaadin.flow.component.HasValue) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) Person(com.vaadin.flow.tests.data.bean.Person) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) StringToDoubleConverter(com.vaadin.flow.data.converter.StringToDoubleConverter) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) NumberFormat(java.text.NumberFormat) Assert.assertSame(org.junit.Assert.assertSame) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) Map(java.util.Map) After(org.junit.After) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) UI(com.vaadin.flow.component.UI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Assert.assertNotNull(org.junit.Assert.assertNotNull) DecimalFormat(java.text.DecimalFormat) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) StringToBigDecimalConverter(com.vaadin.flow.data.converter.StringToBigDecimalConverter) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Serializable(java.io.Serializable) Objects(java.util.Objects) Converter(com.vaadin.flow.data.converter.Converter) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Stream(java.util.stream.Stream) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Assert(org.junit.Assert) Binding(com.vaadin.flow.data.binder.Binder.Binding) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Sex(com.vaadin.flow.tests.data.bean.Sex) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) HasValue(com.vaadin.flow.component.HasValue) Test(org.junit.Test)

Example 2 with HasValue

use of com.vaadin.flow.component.HasValue in project flow by vaadin.

the class Binder method bindProperty.

/**
 * Binds {@code property} with {@code propertyType} to the field in the
 * {@code objectWithMemberFields} instance using {@code memberField} as a
 * reference to a member.
 *
 * @param objectWithMemberFields
 *            the object that contains (Java) member fields to build and
 *            bind
 * @param memberField
 *            reference to a member field to bind
 * @param property
 *            property name to bind
 * @param propertyType
 *            type of the property
 * @return {@code true} if property is successfully bound
 */
private boolean bindProperty(Object objectWithMemberFields, Field memberField, String property, Class<?> propertyType) {
    Type valueType = GenericTypeReflector.getTypeParameter(memberField.getGenericType(), HasValue.class.getTypeParameters()[1]);
    if (valueType == null) {
        throw new IllegalStateException(String.format("Unable to detect value type for the member '%s' in the " + "class '%s'.", memberField.getName(), objectWithMemberFields.getClass().getName()));
    }
    if (propertyType.equals(GenericTypeReflector.erase(valueType))) {
        HasValue<?, ?> field;
        // Get the field from the object
        try {
            field = (HasValue<?, ?>) ReflectTools.getJavaFieldValue(objectWithMemberFields, memberField, HasValue.class);
        } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
            // If we cannot determine the value, just skip the field
            return false;
        }
        if (field == null) {
            field = makeFieldInstance((Class<? extends HasValue<?, ?>>) memberField.getType());
            initializeField(objectWithMemberFields, memberField, field);
        }
        forField(field).bind(property);
        return true;
    } else {
        throw new IllegalStateException(String.format("Property type '%s' doesn't " + "match the field type '%s'. " + "Binding should be configured manually using converter.", propertyType.getName(), valueType.getTypeName()));
    }
}
Also used : Type(java.lang.reflect.Type) InvocationTargetException(java.lang.reflect.InvocationTargetException) HasValue(com.vaadin.flow.component.HasValue)

Aggregations

HasValue (com.vaadin.flow.component.HasValue)2 UI (com.vaadin.flow.component.UI)1 Binding (com.vaadin.flow.data.binder.Binder.Binding)1 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)1 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)1 Converter (com.vaadin.flow.data.converter.Converter)1 StringToBigDecimalConverter (com.vaadin.flow.data.converter.StringToBigDecimalConverter)1 StringToDoubleConverter (com.vaadin.flow.data.converter.StringToDoubleConverter)1 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)1 IntegerRangeValidator (com.vaadin.flow.data.validator.IntegerRangeValidator)1 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)1 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)1 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)1 Person (com.vaadin.flow.tests.data.bean.Person)1 Sex (com.vaadin.flow.tests.data.bean.Sex)1 Serializable (java.io.Serializable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Type (java.lang.reflect.Type)1 BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1