AleXeNoN
AleXeNoN

Reputation: 159

Vaadin Binder set bean just on save operation


I have a similar question like this, but I couldn't find any examples, at all, for form validation with revert option using vaadin binder.

Suppose we have a form with Save and Cancel operations.
I want on save() method the binder actually to update the bean, but on cancel() method, it should do nothing with the bean and leave it as it was, with previous value.

Here are my main questions:

I tried something like this, but the bean is always null on save operation

        private void initBinder() {
            binder.writeBeanAsDraft(myBean);
            binder.forField(inputField)
                    ....
        }
        
        private void save() {
            binder.writeBeanIfValid(myBean);
            binder.readBean(myBean); // retrieve the bean, to save it
            ...
            // save the bean to database
        }
        
        private void cancel() {
            binder.getBean(); // retrieve unchanged bean
            // resets the old values
            inputField.setValue(binder.getBean().getValue());
        }

Any help will great, thanks in advance!

Upvotes: 1

Views: 42

Answers (1)

Tatu Lund
Tatu Lund

Reputation: 10643

The method Binder#getBean returns null when using buffered mode, i.e. using readBean and writeBean.

The Binder#readBean method will populate the bound fields with values from the bean.

The method Binder#writeBean will attempt to update the properties of bean from values of the bound fields provided they pass the validation and conversion. There variants of this method like Binder#writeBeanIfValid, Binder#writeBeanAsDraft ... for specific use cases.

Typically you should implement the revert changes operation by using readBean(bean).

Upvotes: 3

Related Questions