SPCascadeDropdowns with SPSimpleToComplexDropdown causes lookup validation error.

Jul 20, 2011 at 7:31 PM

Hi - we've implimented SPCascadingDropdowns for a set of columns on our site;
Category 1
Category 2
Category 3

We're also using the simpleChild option (SPSimpleToComplexDropdown). The three Category fields are not required, so the first row contains for example "Choose Category 2...".

The problem we're seeing is when the user does not select a value for one or more of the fields, Sharepoint's lookup validation fails:
Value is not among the set of valid lookup values.

The problem only happens when the simpleChild options is enabled and only for complex dropdowns (> 20 rows). I've looked over the SPSimpleToComplexDropdown code and the issue seems to be that the selected text for the first row is not actually a value in the hidden Input's "choices" attribute. Here's the code:

// When the simple select changes...
$("#" + simpleSelectId).change(function() {
	var thisVal = $(this).val();
	// ...set the optHid input element's value to the valus of the selected option...
	$("input[id='" + columnOptHid + "']").val(thisVal);
	// ...and save the selected value as the hidden input's value
	$(columnSelect.Obj).val($(this).find("option[value='" + thisVal + "']").html());
});

We are using SP Services version 0.6.2 alpha 4. There have been other similar posts and a few indicate that this problem may have been resolved, but that's not what we are seeing.

The following code change will fix the problem, but only if the promptText option is set to "(None)". Basically, it looks for the value (None) and changes it to an empty string.

// When the simple select changes...
$("#" + simpleSelectId).change(function() {
	var thisVal = $(this).val();
	// ...set the optHid input element's value to the valus of the selected option...
	$("input[id='" + columnOptHid + "']").val(thisVal);
	// ...and save the selected value as the hidden input's value
	//Modified to fix bug when (None) is selected for non-required column. 
	var thisText = $(this).find("option[value='" + thisVal + "']").html();
	if (thisText == "(None)") thisText = "";
	$(columnSelect.Obj).val(thisText);
});

We are hoping that this bug can be fixed in an future release. Thanks.

Coordinator
Jul 20, 2011 at 11:06 PM
Edited Jul 21, 2011 at 2:07 PM

dgilbert:

Can you show me what your calls look like for the column that is causing the issue? You shouldn't need to call SPCascadeDropdowns *and* SPComplexToSimpleDropdown; just set the simpleChild: true, option in SPCascadeDropdowns.

M.

Jul 21, 2011 at 1:20 PM

We are using the simpleChild option. Here are the calls;

$(document).ready(function() {
	$().SPServices.SPCascadeDropdowns({
		relationshipList: "Sales Categories Level 2",
		relationshipListParentColumn: "Level_x0020_2_x0020_Parent",
		relationshipListChildColumn: "Title",
		parentColumn: "Sales Category Level 1",
		childColumn: "Sales Category Level 2",
		promptText: "(None)",
		simpleChild: true,
		selectSingleOption: true,
		debug: false
	});
	$().SPServices.SPCascadeDropdowns({
		relationshipList: "Sales Categories Level 3",
		relationshipListParentColumn: "Level_x0020_3_x0020_Parent",
		relationshipListChildColumn: "Title",
		parentColumn: "Sales Category Level 2",
		childColumn: "Sales Category Level 3",
		promptText: "(None)",
		simpleChild: true,
		selectSingleOption: true,
		debug: false
	});
});

Thanks for helping with this!

Coordinator
Jul 21, 2011 at 1:31 PM
Edited Jul 21, 2011 at 1:45 PM

I think that the issue is that you are supplying a value for the promptText. If you look at the docs for the SPCascadeDropdowns function, you'll see:

promptText
Text to use as prompt. If included, {0} will be replaced with the value of childColumn. The default value is "Choose {0}...".
NOTE: I discourage the use of this option. Yes, I put it into the function, but if the user doesn't make a choice, they get an ugly error because SharePoint doesn't understand it as an option. I've left in in for backward compatibility.

I believe if you pass an empty string as the value fro promptText, things will work better:

promptText: "",

M.

Jul 21, 2011 at 1:38 PM

Initially we were not using the promptText option. But then the code uses the default "Choose {0}..." which causes the problem. The only reason I'm using the promptText now is so that the code fix has a static value to identify.

Coordinator
Jul 21, 2011 at 1:43 PM

As I mention in the docs, I've left the option in new releases only for backward compatibility. If someone is using it and expecting the "Choose {0}" behavior, I don't want to change it on them. However, it was just a bad idea to add it in the first place. I initially added it in a very early version of SPServices before I had much of a user base.

Do yuo have issues if you use promptText: ""?

M.

Jul 21, 2011 at 2:02 PM

Yes, I tried promptText: "" and it did solve the problem. But without any text value for row 0, the dropdowns that had no other values displayed with a very narrow width and our users did not like the way that looked. They wanted to see (None) which is the default SP behavior for a simple dropdown. I'm surprised so few others have noticed this problem - my guess is it's most common to set cascading dropdowns as required fields (in which case there would be no issue).

Coordinator
Jul 21, 2011 at 2:13 PM

Well, SPComplexToSimpleDropdown is a recent addition, so any complexity it adds hasn't had much of a workout yet.

There are a lot of variations on the settings for columns and because some of the SPServices functions operate on multiple columns, there are lots of edge conditions. I've tried to address them as I've found them or as they have come up in the discussions, of course. The trick is to mirror the out of the box SharePoint functionality enough so as not to confuse your users but at the same time offer better UX. Take all of that and remember that my code needs to work well with any custom branding you might have applied, and it's a real challenge!

One thought on the "dropdowns that had no other values displayed with a very narrow width": you could add your own script to check for that condition and add some sort of message for your users. You can always augment the SPServices functions with your own functions to address specifics in your own implementation.

M.

Jul 21, 2011 at 3:06 PM

No criticism of your code - it's very impressive! We'll continue to use our fix. Thanks for the help.

Coordinator
Jul 21, 2011 at 3:30 PM

I didn't take it as criticism. Sometimes I feel like justifying my madness!

M.