diff --git a/framework/widget/dtd/widget-form.xsd b/framework/widget/dtd/widget-form.xsd
index e472334af4..4823e38623 100644
--- a/framework/widget/dtd/widget-form.xsd
+++ b/framework/widget/dtd/widget-form.xsd
@@ -455,7 +455,11 @@ under the License.
- Used to specify a condition that must be true to use this field; the condition should be written using the Java syntax and can operate on values in the form context; if this is used the field will only be put on the field list, and not in the field map meaning that values for this field cannot be overridden.
+
+ Used to specify a condition that must be true to use this field; the condition should be written using the
+ Java syntax and can operate on values in the form context; conditional fields are evaluated in reverse
+ order so the last field defined that evaluates to true is the one that is rendered.
+
diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelForm.java b/framework/widget/src/org/ofbiz/widget/form/ModelForm.java
index 84eb21c869..941c3d4ea3 100644
--- a/framework/widget/src/org/ofbiz/widget/form/ModelForm.java
+++ b/framework/widget/src/org/ofbiz/widget/form/ModelForm.java
@@ -145,11 +145,13 @@ public class ModelForm extends ModelWidget {
*/
protected List fieldList = FastList.newInstance();
- /** This Map is keyed with the field name and has a ModelFormField for the value; fields
- * with conditions will not be put in this Map so field definition overrides for fields
- * with conditions is not possible.
+ /** This Map is keyed with the field name and has a ModelFormField for the value.
*/
protected Map fieldMap = FastMap.newInstance();
+
+ /** Keeps track of conditional fields to help ensure that only one is rendered
+ */
+ protected Set useWhenFields = FastSet.newInstance();
/** This is a list of FieldGroups in the order they were created.
* Can also include Banner objects.
@@ -293,6 +295,8 @@ public class ModelForm extends ModelWidget {
this.onSubmitUpdateAreas = parent.onSubmitUpdateAreas;
this.onPaginateUpdateAreas = parent.onPaginateUpdateAreas;
this.altRowStyles = parent.altRowStyles;
+
+ this.useWhenFields = parent.useWhenFields;
//these are done below in a special way...
//this.fieldList = parent.fieldList;
@@ -596,12 +600,13 @@ public class ModelForm extends ModelWidget {
* @return The same ModelFormField, or if merged with an existing field, the existing field.
*/
public ModelFormField addUpdateField(ModelFormField modelFormField) {
- if (!modelFormField.isUseWhenEmpty()) {
+ if (!modelFormField.isUseWhenEmpty() || useWhenFields.contains(modelFormField.getName())) {
+ useWhenFields.add(modelFormField.getName());
// is a conditional field, add to the List but don't worry about the Map
//for adding to list, see if there is another field with that name in the list and if so, put it before that one
boolean inserted = false;
for (int i = 0; i < this.fieldList.size(); i++) {
- ModelFormField curField = (ModelFormField) this.fieldList.get(i);
+ ModelFormField curField = this.fieldList.get(i);
if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) {
this.fieldList.add(i, modelFormField);
inserted = true;
@@ -615,7 +620,7 @@ public class ModelForm extends ModelWidget {
} else {
// not a conditional field, see if a named field exists in Map
- ModelFormField existingField = (ModelFormField) this.fieldMap.get(modelFormField.getName());
+ ModelFormField existingField = this.fieldMap.get(modelFormField.getName());
if (existingField != null) {
// does exist, update the field by doing a merge/override
existingField.mergeOverrideModelFormField(modelFormField);
@@ -848,7 +853,7 @@ public class ModelForm extends ModelWidget {
// Check to see if there is a field, same name and same use-when (could come from extended form)
for (int j = 0; j < tempFieldList.size(); j++) {
ModelFormField modelFormField = (ModelFormField) tempFieldList.get(j);
- if (!modelFormField.isUseWhenEmpty()) {
+ if (this.useWhenFields.contains(modelFormField.getName())) {
boolean shouldUse1 = modelFormField.shouldUse(context);
for (int i = j+1; i < tempFieldList.size(); i++) {
ModelFormField curField = (ModelFormField) tempFieldList.get(i);