Java Record form binding available in Viritin 2.8.3

Finally last weekend I found a moment to finish an alternative implementation to the core’s Binder for Viritin 2.8. It is always in “non-buffered” mode, which I anyways find the only right way to do databinding (reasoning: your validation logic is best to be written against your DTOs/entities, they are most like “detached” anyways and if not, you can easyly make a copy of those for the form and use things like commons-beanutils or similar helpers in spring to make a copy & synch changes).

In addition to basic “Java Beans” it also supports “Java Records” that all Java hipsters (like me :crazy_face:) are trying to use whenever possible.

Here is an example code snippet from the test sources:

        var personRecord = new Person("Jorma", 70, 69);

        // This class has fields to edit Person properties and
        // how to lay them in the UI
        PersonForm form = new PersonForm();
        add(form);
        // In typical cases, you'll use the dto type for binder and then assigning the value later
        // var binder = new FormBinder<>(Person.class, form);
        // binder.setValue(personRecord);

        // alternatively assign non-null value directly, type is then drawn from it
        var binder = new FormBinder<>(personRecord, form);

        // With a value change listener to the binder we can listen to all fields at once
        binder.addValueChangeListener(event -> {
            // As an example, we validate the current state with
            // Java Bean Validation API
            var violations = validate(binder.getValue());
            // and assign reported violation them to binder, which shows them
            // in the form (if connected to a property, next to its field)
            binder.setConstraintViolations(violations);
        });

Check more from the project’s readme.

I’m quite confident about the design but I’m sure there are many things to improve and fix still, so would be more than happy to get some feedback about it, with or without records!

3 Likes