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());
}
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()));
}
}
Aggregations