I am fairly new to php and I am trying to create a module. I am currently working on an administration page. I have a form that displays a select list for the all the drupal roles that have been added (anonymous user, authenticated user, any moderator or administration role is excluded), if there are no roles I set the select list to display "Please create Role" and if they submit the form with this selected the validation function uses a drupal_set_error to send back an error with a link to the roles section to add a new role. The problem is it works as expected when there has been no roles added, but two things are happening that shouldn't if there is a role added already. If you click on the select list there is a "0" (zero) on top of the role name and 1 if there more then 1 role names added and I still get the drupal_set_error message even though the name does not equal "Please create Role". My code is as follows:
In the freeradius.admin.inc file
function freeradius_dbsettings_form(&$form_state) {
$form = array();
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => 'Default Settings',
);
$form['settings']['enctype'] = array(
'#type' => 'select',
'#title' => 'Default Password Encryption Type',
'#description' => 'This is the password encryption type used by your radius server not by drupal',
'#options' => array(
'Auth-Type' => 'Auth-Type',
'Cleartext-Password' => 'Cleartext-Password',
'CHAP-Password' => 'CHAP-Password',
'ENCRYPT-Password' => 'Encrypt-Password',
'MD5-Password' => 'MD5-Password',
'SHA1-Password' => 'SHA1-Password',
'User-Password' => 'User-Password'),
'#default_value' => isset($form_state['values']['enctype']) ? $form_state['values']['enctype'] : 'Cleartext-Password',
);
$form['settings']['defaultop'] = array(
'#type' => 'select',
'#title' => 'Default Operator',
'#description' => 'This is the default operator used by your radius server (radcheck table in the database supplied below) not by drupal',
'#options' => array(
'1' => '=', '2' => ':=', '3' => '==', '4' => '+=', '5' => '!=', '6' => '>', '7' => '>=',
'8' => '<', '9' => '<=', '10' => '=~', '11' => '!~', '12' => '=*', '13' => '!*'),
'#default_value' => isset($form_state['values']['defaultop']) ? $form_state['values']['defaultop'] : '2',
);
// Do not allow users to set the anonymous or authenticated user roles as the
// default usergroup (role) to be added to freeradius database tables.
$roles = user_roles();
$options = freeradius_get_roles();
if (empty($options)) {
$options = 'Please create Role';
}
$form['settings']['usergroup'] = array(
'#type' => 'select',
'#title' => t('Default freeradius UserGroup'),
'#options' => array($options),
'#default_value' => isset($form_state['values']['usergroup']) ? $form_state['values']['usergroup'] : ' ',
'#description' => t('This role will used as the default UserGroup for freeradius for new registrations.'),
);
$form['dbsettings'] = array(
'#type' => 'fieldset',
'#title' => 'Database Settings',
);
$form['dbsettings']['dbname'] = array(
'#type' => 'textfield',
'#title' => t('Database name'),
'#description' => t('The database name that the FreeRadius will use. This database needs to already be created and set up by someone with permissions to do so.'),
'#required' => TRUE,
'#default_value' => isset($form_state['values']['dbname']) ? $form_state['values']['dbname'] : ' ',
'#size' => 25,
);
$form['dbsettings']['dbuser'] = array(
'#type' => 'textfield',
'#title' => t('Database username'),
'#description' => t('The database username that is required to connect to the FreeRadius database.'),
'#required' => TRUE,
'#default_value' => isset($form_state['values']['dbuser']) ? $form_state['values']['dbuser'] : ' ',
'#size' => 16,
);
$form['dbsettings']['dbpass'] = array(
'#type' => 'password',
'#title' => t('Database password'),
'#description' => t('The database password that is required to connect to the FreeRadius database.'),
'#required' => TRUE,
'#size' => 16,
);
$form['dbsettings']['dbhost'] = array(
'#type' => 'textfield',
'#title' => t('Database host'),
'#description' => t('The database hostname that is required to connect to the FreeRadius database. The hostname localhost will be assumed if left blank.'),
'#default_value' => isset($form_state['values']['dbhost']) ? $form_state['values']['dbhost'] : 'localhost ',
'#size' => 50,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => 'Reset',
);
return $form;
}
the validation & get_roles functions from the freeradius.module
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)) {
$rows[] = $row['name'];
}
return $rows;
}
function freeradius_dbsettings_form_validate(&$form, &$form_state) {
// Set up variables for the values of our 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 ($form['usergroup']['#value'] = 'Please create Role') {
form_set_error('usergroup', 'Please visit the <a href="!rolespage">Roles</a> page and add at least one role other then "anonymous user ,authenticated user or administrator".', array('!rolespage' => url('admin/user/roles')));
}
// 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';
}
}
any help any one can shed on this issue would be greatly appreciated.
Comments
'#options' =>
'#options' => array($options),Methinks, sir, here you are creating an array of one array, because $options coming from freeradius_get_roles() is already an array. Should be:
'#options' => $options,However, you should ensure it's always an array, so:
Yes that was exactly it, I
Yes that was exactly it, I was banging head trying to figure this out and was totally in the wrong file, I was in the module file looking at the freeradius_get_roles function. This fixed the 0 in the select list. But I think I need to look at that function (freeradius_get_roles) for select list stores the values as 0,1,2 etc but I need a way to check if value is 0 && name = "Please create Role".