I am not sure the best way to work with form values so i tend to approach it as i would with Drupal's php forms; but that doesn't work within Drupalgap afaik.

i have a routine which adds nids to an array for storage into local storage. without putting too much thought into it i store a simple array like this:

nids = [34, 45, 64];
variable_set('nids', nids); 

i later want to use this list as options for a checkboxes form element and i set this up as this:

function mynids_form(form, form_state) {
  
  var nids = variable_get('nids', {});
 // need to convert the json string that getts stored in local storage 
 if (nids.length) {
    nids = JSON.parse(nids);
  }
  nids = $.extend({}, nids);
  form.elements['my-nids'] = {
    title: 'My NIDs',
    type: 'checkboxes',
    options: nids
  };
  form.elements['submit'] = {
    type: 'submit',
    value: 'Remove from NIDs'
  };
  
  return (form);
}

although a bit of a pita to deal with the json string, this generally seems to work and i now get a set of checkboxes with the labels:

34
45
64

but, when i submit this form, the values in form_state['values']['my-nids'] loses the checkbox values and i only retain the keys:

0:0
1:0
2:0

if i jump through a few more hoops when i am storing the array and store as an associated array (object) so i am storing this:
{34:34, 45:45, 64:64}

then when i submit my form i get this for form_state['values']['my-nids']:

34:0
45:0
64:0

so, i guess i could now do an array_keys (whatever js equivalent is) to get the key values; but this seems like much more work than it needs to be.

Comments

tyler.frankenstein’s picture

Status: Active » Postponed (maintainer needs more info)

The options expected when building a select list is a JSON object {value: 'label'}, for example:

{34:34, 45:45, 64:64}

Then when the form is submitted, the form state value should come through as a single value, according to whatever option was selected. For example, if you select the option for 34, the value in the form state should be 34.

Unless it is a multi value select list, then I think it would come through like this:

{34:1, 45:0, 64:0}

It's possible there is a bug(s) with selects/options/form_states, but last I checked it seemed pretty stable...

liquidcms’s picture

ok, i see.. must have been my bad test.. i think i only tested with 2 options and picked the 2nd one for submit test.. so it would have been

0:0
1:1

i'll recheck.

odd that you say it needs a json string rather than an array though but will retest that as well. pretty sure i am passing it an array (and with more effort, an assoc array)

i am sure this makes sense how this is done; but for reference, i think the way drupal (php) does it is simply passing to submit an array of the checked values. perhaps a use case where you need the unchecked ones; but options being a simple array and passing a simple array would be much easier to work with.

liquidcms’s picture

yea, rechecked, what you said isn't quite right.

i need to pass an associative array for checkboxes options and i get back keyed array with non selected values of 0 and selected as the items key

so, e.g.:

for nids [34, 45]

i have to pass:
{34:'34', 45:'45'}

and if i select 45 i get values in my submit function as:
{34: 0, 45:'45'} (not what i thought i had last night)

so its manageable, but seems a lot more effort than it could be. imho.. :)

tyler.frankenstein’s picture

Yeah arrays/objects are a bit strange in JS. AFAIK, there really isn't such a thing as an "associative array" in JS, instead we use JSON objects (which are basically an associate array). Hence, we have to pass a JSON object as the options or else we can't have non integer values placed in the options.

If the form state value is coming through as {34: 0, 45:'45'}, that makes me think multiple values are allowed, and not just a single value.

If it is a single value, it should just come through as the value 45, it's possible that there is a bug though...

liquidcms’s picture

it is a checkboxes field.. so multiple by default

tyler.frankenstein’s picture

Thanks for clarifying, I missed your part earlier about checkboxes and my brain was wired to thinking about select lists for some reason. As for checkboxes, then yes they probably have some quirks/bugs. When I implemented them they weren't tested too deply, so may need some TLC. But if I recall correctly, I tried to model their form state values to how Drupal's FAPI models them.