Hi All,

I think i'm doing something stupid here but i'm having checkbox issues.

Im passing a number of values into a foreach, on each loop a check box is built. the '#return_value' is always a string, that string can have spaces in it, underscores, both or nether.

heres the issue:

apparently on form submission only the strings that have -no- spaces in are being submitted as values (as expected). the rest return a 0 - reguardless of whether they where selected or not.

some of the values being submitted (in $perm)

administer mmi_user_core --- Always 0
view skillsheets --- Always 0
change_all_corp_api_settings -- Works fine (returns change_all_corp_api_settings when checked, 0 when not)

the checkbox form item: (the rest of the form is huge, this is where the issue is. the Watchdog call see's $perm set fine just before.

          mmi_watchdog('role names', '', $perm);
          // Builds arrays for checked boxes for each role
          $form['corp-roles'][$rowrid.':'.$perm] = array(
          	'#type' => 'checkbox',
          	'#title' => '',
          	'#return_value' => $perm,
          	'#default_value' => (isset($role_permissions[$rowrid][$perm]) ? $perm : 0),
          	'#prefix' => '<td>',
          	'#suffix' => '</td>',
          );

getting the values: $value is always 0 if there where spaces in the $perm string

/**
* form submit handler
*/
function mmi_corp_roles_settings_alter_submit($form, &$form_state){
	mmi_watchdog('corp submit start', 'form_state', $form_state['values']);
	foreach($form_state['values'] as $fields => $value){
		if(strpos($fields, ':') !== FALSE){
			list($rid, $perm) = explode(':', $fields);
			mmi_watchdog('corp submit', $rid, array($perm => $value));
			user_role_change_permissions($rid, array($perm => $value));
		}
	}
}

for anyone interested, mmi_watchdog: (before someone thinks im trying to do a hook_watchdog call....)

/**
* Added an extra peramiter $array that gets appended to the $message as passed to watchdog. Can be Array or Object
* Custom implimentation of watchdog to <pre> format arrays and objects as passed in for better and cleaner logging.
* see http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/watchdog/7 for input. where $array can be String, Array or Object
*/
function mmi_watchdog($type, $message, $array, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL){
    ob_start();
    echo "<pre>";
    print_r($array);
    echo "</pre>";
    $array = ob_get_contents();
    ob_clean();
    watchdog($type, $message.$array, $variables, $severity, $link);
}

anyone have any ideas? shout for any questions etc

Comments

redsd’s picture

The easiest solution is probally:

          $perm = str_replace(" ", "[space]", $perm);
          mmi_watchdog('role names', '', $perm);
          // Builds arrays for checked boxes for each role
          $form['corp-roles'][$rowrid.':'.$perm] = array(
              '#type' => 'checkbox',
              '#title' => '',
              '#return_value' => $perm,
              '#default_value' => (isset($role_permissions[$rowrid][$perm]) ? $perm : 0),
              '#prefix' => '<td>',
              '#suffix' => '</td>',
          );

and

/**
* form submit handler
*/
function mmi_corp_roles_settings_alter_submit($form, &$form_state){
    mmi_watchdog('corp submit start', 'form_state', $form_state['values']);
    foreach($form_state['values'] as $fields => $value){
        if(strpos($fields, ':') !== FALSE){
            list($rid, $perm) = explode(':', $fields);
            $perm = str_replace("[space]", " ", $perm);
            mmi_watchdog('corp submit', $rid, array($perm => $value));
            user_role_change_permissions($rid, array($perm => $value));
        }
    }
}

So just use a replacement for the spaces.

N1ghteyes’s picture

Ok, so it seems like there are character limitations on this. where only a-z0-9_ are allowed. (would need testing)

So with a slightly different replacement (_s_) and a small adjustment to the submit handler its now working.

Thanks :) that was doing my head in.

redsd’s picture

Good to hear it works, didn't know about those limitations either.

Never used special characters in the forms :)