SelectItem change handler

So I’ve got a change handler on a select box, taking a specific action when a user chooses a new value, and then I return true from the change handler. I’ve also made the select required, so they have to pick something before submitting. This works just dandy if my line item already has a type and the user doesn’t change it. However, if a user picks a new value and tries to submit, the valuesmanager form validation thinks that the type is empty and doesn’t let the form submit. Here is my change handler for the type select:

{name: “TypeId”, title:“Tool Type”,
required: true,
type: “select”,
showOptionsFromDataSource: true,
optionDataSource: “toolTypesDS”,
valueField: “Id”,
displayField: “Value”,
change: function (form, item, value, oldValue) {

        	resetTypeTab();
        	
			initializeTypeForm(value);
							
			return true;
		}
	}

and how I trigger a save:

function saveTool() {
toolFormValuesManager.clearErrors(true);
toolFormValuesManager.validate();

if (toolOverviewForm.hasErrors()) { 
	toolTabSet.selectTab(0);
} else {
	toolFormValuesManager.saveData();
}

}

What am I doing wrong that my form thinks the type select is empty when a user tries to select something else?

Hi jjohns,

I suspect the problem is in the code you didn’t show, that the change() handler is calling. Does removing the change() handler solve the problem? If so, perhaps the change() handler calls setValues(), or rebuilds the form or ValuesManager?

Other possibilities are that your related DataSource (toolTypesDS) does not actually have a field “Id”, or it’s capitalization is different, or the particular records loaded in this case do not have a value for “Id”.

Also, if you check the ValuesManager.getValues() before calling validate(), do you see a value for the “TypeId” field?

When the change handler is removed the problem does indeed go away. I am not calling setValues() from the change handler or anything the change handler calls, I am however adding a form to the values manager. That is the only thing I could think of that is affecting this. I’ve got several other select boxes on the same form as the TypeId and they are not exhibiting this issue.

The “TypeId” field is indeed missing if the change handler is triggered by looking at the getValues() right before validation.

The toolTypesDS is working correctly with all the right names and such, as I can see it working and setting values correctly when the change handler is disabled.

To ensure that adding the form dynamically to the ValuesManager is the problem, can you check the ValuesManages values immediately before and after the form is added?

If the dynamic addition of a form is the problem, things to check:

Does the form you dynamically add to the ValuesManager have a colliding field (another field named “TypeId”, possibly even hidden)?

Do you initialize it with form.values, or call setValues() on it before or after it is added to the ValuesManager?

Is it drawn at the time it’s added, or does it draw later?

Well, I’ve gone ahead and changed the flow of the screen to have the user select the type before anything is done. That way I don’t have to worry about altering the values manager mid edit in a form, I can set up all forms once the user has chosen the type and not worry about it. Thus the problem of the required select box type chooser without a value goes away…

Good to hear you have a working solution. If you happen to find out what the actual problem was, please let us know.

I made some attempts at recreating the interaction you are talking about (see below), but everything worked correctly. The only wrinkle I discovered, which you might not be aware of, is that if you check the form’s values on change(), the new value has not been committed yet (because you are allowed to cancel change() by returning false). So if you manipulated the values immediately on change() you might have stopped the new value from being committed.

isc.DynamicForm.create({
   ID:"theForm",
    fields: [
        { name: "itemID", type:"select",
          optionDataSource:"supplyItem",
          idField:"itemID", valueField:"itemName",
          change: function () { 
              this.logWarn("change fired, values: " + this.echo(theVM.getValues()));
              theVM.addMember(secondForm); 
              this.logWarn("after VM.addMember, values: " + this.echo(theVM.getValues()));
              isc.Timer.setTimeout("isc.Log.logWarn(isc.Log.echo(theVM.getValues()))");
          }
        }
    ]
}); 

isc.ValuesManager.create({ ID:"theVM", members:[theForm] });

isc.DynamicForm.create({
   ID:"secondForm",
   top:250,
   values: {},
   fields: [
      { name:"someField" },
      { name:"someOtherField" }
   ]
});