I have a checkboxfield with a form variable as dataprovider. This checkboxfield gets populated with a valuelist.
I’m a bit confused with reading the selected values. The form variable outputs the selected values as a single entry. In other words: there is no array even though I have selected multiple items. I could split the variable into an array by .split(‘\n’), although problematic when just one item is selected.
I have this feeling that I’m doing something wrong. What is the best way to simply populate an array with the selected values from the checkbox?
Maybe not problematic, but the split function gives an error when there is no line-break (if i split it by ‘\n’). So i have to make an if-else statement. This made me wonder if I was working along the correct path, because it seems so unlogical to manually split it to an array.
It shouldn’t give an error when you use a string with a value. Not even with an empty string.
What probably happens is that your field doesn’t have any value, i.e. a null. The split() function only works on strings so it will then cause an error.
Can you check ?
Well, if I don’t select anything, so the variable is null, I get the following error (like you said it will):
TypeError: Cannot call method "split" of null
If i just select 1 item, I get the following error: (very strange, right?)
TypeError: Cannot find function split.
But, when 2 or more items are selected everything works perfectly. I’m very confused about this, that’s why I got a bit insecure about the way I was approaching this.
This is what I have in my method:
var itemsArray = new Array();
itemsArray = selectedItems.split('\n');
Do you use a valuelist with integers as real values perhaps ?
If so use this:
var itemsArray = String(selectedItems).split('\n');
Also is selectedItems a variable or a dataprovider ? If a variable then make sure it is set to an empty string and not a null.
If it is a dataprovider (i.e. a table column) set the default value to an empty string.