Set field as not modified

Hi,

I’m trying to check when an edit form is modified or not, to enable or disable the submit button.
The problem is that I have a ComboBox, which I initialise with the possible values in the @PostConstruct method in the form. Then the field gets marked as changed, when the only think I did is to fill it with the values and select the correct one.
I tried to find a way to mark it back to not changed, so when I call the isModified() method, the values is false and the button is not enabled.

Any idea on how to accomplish this?

Thanks

Hello Enkara,

you could add a ValueChangeListener at the end of your @PostConstruct method. When value is changed, your form has been modified.

But how do I check that the value has actually been changed? The isModified always returns true for the ComboBox field. Before initialising the combobox with the values isModified is false, but after that is true. I need to initialise the ComboBox, but how do I do it so isModified still returns false?

Is there something I am doing wrong? I can’t believe that the method isModified() in all the forms that have a ComboBox will return true no matter what after setting the ContainerDataSource.

I would appreciate if anybody could check if after setting the ContainerDataSource of a ComboBox, the isModified() method returns true. Then I could identify if I am doing something wrong or if it works like this by design, making the isModified method useless when there is a ComboBox

Hello Enkara,

as I said you could just use your own ValueChangeListener listening if the value has been changed, changing your submit button state event based.
Also you can commit your ComboBox after filling and setting with the value. After the the commit() call the modified state should be false and you can check if the state was changed after initializing.

best regards
Wolfgang

Enkara, do you bind the field withe the property
after
fulfilling the combo with values? If not, the selecting any item, marks the field as modified.

Oh! Thank you Agata! I see where my problem is now. The field binding is done before fullfilling the combo with values. We have an elaborated way to create the forms.

Wolfgang thank you for your answer again too, the problem with making my own ValueChangeListener is that I needed to add a lot of extra code and it really couldn’t be that Vaadin had this problem with the ComboBoxes. And the commit advice was helpful!

One more question about this…

I extended ComboBox so if it has only one possible value, that is automatically selected.
If I initialize the values of the combo before binding, the value i had selected is not sleected anymore. If I do it later, then the form is modified. The way to solve this is to do it after and then use commit()?

If your bean has the field value as the only one element of combo box, it should be selected during binding without marking the field is modified. But if your bean has no value or another one, you should select it manually.

I’m sorry, I don’t understand your answer.
The form has a ComboBox. I have to initialize that ComboBox with values, but if there is only one value possible I want that value selected. If I select it before binding, then it gets unselected during binding. If I select it after binding, then the form is marked as modified. How can I solve this? Either there is a way to keep the value selected when binding or there is a way to mark the form as not modified after binding (commit()?).

Binding associates UI fields with bean fields, so during binding there is selected value from the bean. If the value is not present in combobox or the field value is null, nothing is selected.
When you fullfil a combo, that is bound to a field, with values and select one of them, the bean field value is “changed” (even if the value is the bean field value), so the form is marked as
dirty.

You can’t change the bean field value and not have your form dirty. If you change the bean, you need to call commi() to have a bean field group in not dirty sate.

Thank you for the detailed explanation. I now know the possibilities I have.