I am fairly new to php, and have done multiple searches to try and fix my problem and I either not searching on the right thing or I am not finding the answer I need. I have a form with a select list that is generated from a function that gets a list of roles from the drupal roles table (just the name), but it excludes the two roles that are set up by default (anonymous user and authenticated user) as well as any role that looks like it has been added for the purpose of moderating or administration. I realize that a select list values are stored as numeric values that increment for each item starting at 0 and going until last item has a number. What I am having trouble with is when you call $form_state['values']['form_item'] the value will be whatever number item is selected, but how do you get the text value that corresponds to that number? I need this because if there are no roles returned from the function I set a value and I need to construct a if statement to check if $form_state['values']['form_item'] = 0 && ?? name = something.

Unfortunately I do not know how to return the text value of the select list. I have tried with $form['form_item']['#value'] and $elements['#value'] and either I doing it wrong or have an error for it doesn't seem to work.

Comments

yaworsk’s picture

I am assuming you are doing this in either a submit or validate function... if you have devel enabled (which i recommend you do), add dsm($form) in the function -- the options are listed there and you can pull the text from it, not at a computer i can confirm this on but I think it is $form[#options] or #values like you mentioned, something like that depending on how your form is configured.

krazykanuk’s picture

Yes I was using it in a validate function, sorry I should have stated that. I have the devel module set up in a block, but I was using it (dsm) with the variables I was creating which were returning empty. You were correct it was $form['#options'], but even with that it is only half working. What I mean is when you have no roles (other then the ones I am excluding) it works giving the error as I expect, but if you add roles it gives the error

Fatal Error: Maximum function nesting level of '100' reached aborting in includes/form.inc 1433

I seem to be either to get it to work with no roles and then have to switch code to get it to work with roles, I can't seem to get it to work with both. I believe it is the freeradius_get_roles() function that is causing it.

freeradius.admin.inc.php


$form['settings']['usergroup'] = array(
  '#type' => 'select',
  '#title' => t('Default freeradius UserGroup'),
  '#options' => $options, //change this from $options, to array($options), to get it working with or without roles added
  '#description' => t('This role will used as the default UserGroup for freeradius for new registrations.'),
);

freeradius.module.php


function freeradius_get_roles() {
    $options = db_query("SELECT name FROM {role} WHERE name <> '%s' AND name <> '%s' AND name NOT LIKE '%s' AND name NOT LIKE '%s'", "anonymous user", "authenticated user", "mod%%", "admin%%");
    while ($row = db_fetch_array($options)) { //switch this from db_fetch_object to db_fetch_array (depending how freeradius.admin.inc is set)
        $rows[] = $row['name'];
    }
    return $rows;
}

function freeradius_dbsettings_form_validate(&$form, &$form_state) {
    global $user;
    // Set up variables for the values of are fields
    variable_set('dbname', 'dbname');
    variable_set('dbuser', 'dbuser');
    variable_set('dbpass', 'dbpass');
    variable_set('dbhost', 'dbhost');
    variable_set('enctype', 'enctype');
    variable_set('defaultop', 'defaultop');
    variable_set('usergroup', 'usergroup');

    // If the value for dbname is empty give a error showwing it required
    if (is_null($form_state['values']['dbname'])) {
        form_set_error('dbname', t('The database name is a REQUIRED field.'));
    }

    // If the value for dbuser is empty give an error showing it is required
    if (is_null($form_state['values']['dbuser'])) {
        form_set_error('dbuser', t('The database username is a REQUIRED field.'));
    }
    
    // If the value of dbpass is empty give an error showing it is required
    if (is_null($form_state['values']['dbpass'])) {
        form_set_error('dbpass', t('The database password is a REQUIRED field.'));
    }
    // If the value of dbhost is not empty and it is numeric give an error saying it shouldn't be numeric
    if (!is_null($form_state['values']['dbhost']) && is_numeric($form_state['values']['dbhost'])) {
        form_set_error('dbhost', t('The value entered here should not be numeric.'));
    }
    
    // If the value of dbhost is left blank set it to localhost
    else {
        $form_state['values']['dbhost'] = 'localhost';
    }
    
    $usergroup = $form['#options'];
    if (($form_state['values']['usergroup'] == 0) && ($usergroup = 'Please create Role')) {
        form_set_error('usergroup', 'Please visit the <a href="/admin/user/roles">Roles</a> page and add at least one role other then "anonymous user ,authenticated user moderator or administrator".');
    }
}
krazykanuk’s picture

I managed to fix it so it will display with out errors for both when there no roles set and when there is roles set. I did it by changing the form:

from this:

$form['settings']['usergroup'] = array(
  '#type' => 'select',
  '#title' => t('Default freeradius UserGroup'),
  '#options' => $options, //change this from $options, to array($options), to get it working with or without roles added
  '#description' => t('This role will used as the default UserGroup for freeradius for new registrations.'),
);

to this:

// Do not allow users to set the anonymous user, authenticated user
    // or roles that look like moderator or administrator roles as the
    // default usergroup (role) to be added to freeradius database tables.
    $options = freeradius_get_roles();
    if (empty($options)) {
        $options = array('0' => t('Please create Role'));
    }
    $form['settings']['usergroup'] = array(
        '#type' => 'select',
        '#title' => t('Default freeradius UserGroup'),
        '#default_value' => variable_get('usergroup', '0'),
        '#options' => $options,
        '#description' => t('This role will used as the default UserGroup for freeradius for new registrations.'),
    );

I also changed the freeradius_get_roles function from this:

function freeradius_get_roles() {
    $options = db_query("SELECT name FROM {role} WHERE name <> '%s' AND name <> '%s' AND name NOT LIKE '%s' AND name NOT LIKE '%s'", "anonymous user", "authenticated user", "mod%%", "admin%%");
    while ($row = db_fetch_array($options)) { //switch this from db_fetch_object to db_fetch_array (depending how freeradius.admin.inc is set)
        $rows[] = $row['name'];
    }
    return $rows;
}

to this:

function freeradius_get_roles() {
    $result = db_query("SELECT name FROM {role} WHERE name <> '%s' AND name <> '%s' AND name NOT LIKE '%s' AND name NOT LIKE '%s'", "anonymous user", "authenticated user", "mod%%", "admin%%");
    while ($o = db_fetch_object($result)) {
        $rows[] = $o->name;
    }
    return $rows;
}

I found this solution here http://drupal.org/node/730796. The problem is that with the following if statement in the validate function it works if there no roles as expected (gives the form_set_error) but when there are roles added and shouldn't be failing it does (by also giving the form_set_error).

function freeradius_dbsettings_form_validate(&$form, &$form_state) {
    if (($form['#options'] = 'Please create Role') && ($form_state['values']['usergroup'] == '0')) {
        form_set_error('usergroup', 'Please visit the <a href="/admin/user/roles">Roles</a> page and add at least one role other then "anonymous user ,authenticated user or administrator".');
        dsm($form);
        dsm($form_state);
    }