Form interaction when setItemDatasource

Hi guys,

I’m using Form to edit a bean, and I have some field interaction logic in createField function.
Let’s say I have field1, field2, if field1 has a specified value, field 2 will be disabled.
It all works well except first time the data item is set to the form.

After debugging, I found In Form::setItemDatasource, once field is created, the setPropertyDatasource is called on the new field, and the value change event is fired.
At this time, field2 has not been created, therefore, there’s no chance that to set it disabled or enabled.

My question is whether there is a elegant solution to resolve this kind of issue?

Hi Oliver!

You could solve this kind of problem e.g. as following. This is ofcourse just a one possible solution.

(1) Create some dependency class that will contain the needed dependency between the fields… It could be something like

 
class BetweenFieldsEnableDependency { 
   private String firstFieldsPropertyId; 
   private String secFieldsPropertyId; 
   private Object triggeringValueOfFirstField;
   // ... getters setters etc.
 } 

(2) Make your own implementation class of FormFieldFactory interface or at least override the createField(final Item item, final Object propertyId, final Component uiContext) method.

(3) In createField method go through all possible BetweenFieldsEnableDependency objects and check whether the dependency triggers the field enablation/disablation for e.g. field2

(4) Add also similar dependency checking logic into all field’s valueChangeListeners.