use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced.
@Test
public void bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced() {
BindWithNoFieldInPerson form = new BindWithNoFieldInPerson();
Binder<Person> binder = new Binder<>(Person.class);
TestTextField name = new TestTextField();
form.firstName = name;
binder.forField(form.firstName).withValidator(new StringLengthValidator("Name is invalid", 3, 10)).bind("firstName");
TestTextField ageField = new TestTextField();
form.noFieldInPerson = ageField;
binder.forField(form.noFieldInPerson).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
binder.bindInstanceFields(form);
Person person = new Person();
String personName = "foo";
int age = 11;
person.setFirstName(personName);
person.setAge(age);
binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Assert.assertEquals(String.valueOf(person.getAge()), form.noFieldInPerson.getValue());
// the instances are not overridden
Assert.assertEquals(name, form.firstName);
Assert.assertEquals(ageField, form.noFieldInPerson);
// Test correct age
age += 56;
form.noFieldInPerson.setValue(String.valueOf(age));
Assert.assertEquals(form.noFieldInPerson.getValue(), String.valueOf(person.getAge()));
// Test incorrect name
form.firstName.setValue("aa");
Assert.assertEquals(personName, person.getFirstName());
Assert.assertFalse(binder.validate().isOk());
}
use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderTest method withConverter_disablesDefaulNullRepresentation.
@Test
public void withConverter_disablesDefaulNullRepresentation() {
Integer customNullConverter = 0;
binder.forField(ageField).withNullRepresentation("foo").withConverter(new StringToIntegerConverter("")).withConverter(age -> age, age -> age == null ? customNullConverter : age).bind(Person::getSalary, Person::setSalary);
binder.setBean(item);
Assert.assertEquals(customNullConverter.toString(), ageField.getValue());
Integer salary = 11;
ageField.setValue(salary.toString());
Assert.assertEquals(11, salary.intValue());
}
use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderTest method do_test_save_bound_beanAsDraft.
private void do_test_save_bound_beanAsDraft(boolean setBean) {
Binder<Person> binder = new Binder<>();
binder.forField(nameField).withValidator((value, context) -> {
if (value.equals("Mike")) {
return ValidationResult.ok();
} else {
return ValidationResult.error("value must be Mike");
}
}).bind(Person::getFirstName, Person::setFirstName);
binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
Person person = new Person();
if (setBean) {
binder.setBean(person);
}
String fieldValue = "John";
nameField.setValue(fieldValue);
int age = 10;
ageField.setValue("10");
person.setFirstName("Mark");
binder.writeBeanAsDraft(person);
// name is not written to draft as validation / conversion
// does not pass
assertNotEquals(fieldValue, person.getFirstName());
// age is written to draft even if firstname validation
// fails
assertEquals(age, person.getAge());
binder.writeBeanAsDraft(person, true);
// name is now written despite validation as write was forced
assertEquals(fieldValue, person.getFirstName());
}
use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderTest method setReadOnly_boundBinder.
@Test
public void setReadOnly_boundBinder() {
binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
binder.setBean(new Person());
binder.setReadOnly(true);
assertTrue(nameField.isReadOnly());
assertTrue(ageField.isReadOnly());
binder.setReadOnly(false);
assertFalse(nameField.isReadOnly());
assertFalse(ageField.isReadOnly());
}
use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderValueChangeTest method userOriginatedUpdate_unbound_singleEventOnSetValue.
@Test
public void userOriginatedUpdate_unbound_singleEventOnSetValue() {
TestTextField field = new TestTextField();
binder.forField(field).bind(Person::getFirstName, Person::setFirstName);
binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
binder.addValueChangeListener(this::statusChanged);
Assert.assertNull(event.get());
field.getElement().getNode().getFeature(ElementPropertyMap.class).setProperty("value", "foo", false);
verifyEvent(field, true);
}
Aggregations