I know I am new to php but every single book tutor or manual page I go to says if you are setting the value it should be single = but if you are checking if a variable is equal to something it should be ==. I ask because I have a if statement in a validate function that displays required error if I have single = but passes if I have ==. Basically I have a select list and if there no roles (other then the default ones you get during a drupal install and a few others) then I set it to be a value. Then once I validate I want to check if that value is equal to what I set it to and if it is fail with an error. I can get it to fail and give the error if I use single = but not if I use double ==, but then if I add another role it fails as well and it shouldn't. Below is the form section and function I am calling along with what I have tried.
$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.'),
);
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;
}
function freeradius_dbsettings_form_validate(&$form, &$form_state) {
global $user;
if ($form['#options'] == 'Please create Role') {
// This returns saying form was saved when it should have failed and returned form_set_error below
// if ($form['#options'] = 'Please create Role') {
// above line displays form_set_error but it also does it when I add another role I get the same thing if I switch from ' ' to " "
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".');
dsm($form);
dsm($form_state);
}
Also is there a way to view contents of a dsm(); result in a submit or validate if the function passes all checks?
Comments
Everything you've read is
Everything you've read is right! When you use an assignment in an if statement the condition checked is the result of that assignment. So unless $form['#options'] = 'Please create Role' returns a FALSE/NULL/0 (which it doesn't), the condition for your if statement will always evaluate to TRUE. I think using assignment in an if/while is actually considered bad practice for exactly this reason; it can get very confusing.
The reason your code isn't working though is because you're testing the wrong thing. The #options that you're looking for are in the variable that you set them up to be in: $form['settings']['usergroup']['#options']. If your change you if statement to this you should be OK:
Hope that helps!
Your test if
Your test
if ($form['#options'] == 'Please create Role')does not makes sense since there is no $form['#options'] .I suspect what you want is
or possibly
If you are trying to deal with the case where there are no roles I would change this
to something like
OK all is working fine now, I
OK all is working fine now, I need to tweak what is there but it is working. It was $form['settings']['usergroup']['#options'][0] because form_state['values']['usergroup'] would return the numeric value for the position it is in the select list and there could be multiple selections that would equal 0 (depending if you had any roles or if it was set in the module) but the name would be unique, now that it working I could actually do a if &&. I tried the form using the warning, it worked but found it more prominent if I used form_set_error. I thank you all for your time and effort in helping me.