Jump to content

Recommended Posts

I am creating a module (for drupal) to use the website as a front end and connect to a radius server. I currently have a form that displays a select list. The form calls a function that has a database call to exclude the default roles or any that look like they are for moderating or administering and if this returns empty it puts a default in. This is all working perfectly, problem arises when validating the information. I want it to display an error if they have not created any roles and the select list is showing the default (Please create Role) it is doing it for this, but if I go create two roles the first one it is also showing the error even though it shouldn't, but the second role is running fine and passing the validate. Below is the validate if && statement I am using.

 

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);
}

I have tried reversing $form['#options'] and $form_state['values']['usergroup'] with same results. I am guessing it failing because $form_state['values']['usergroup'] is 0 for both 'Please create role' and for the first 'testrole1' but I thought it would check $form['#options'] first and would not fail because the name would not be 'Please create Role'. The dsm($form) and dsm($form_state) is part of a module where I can see the values when it is failing when it is not expected $form_state['values']['usergroup'] is 0 but $form['#options'] is not 'Please create Role' it is 'testrole1' any help on this matter would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/239663-if-statement-not-working-as-expected/
Share on other sites

Sorry, I didn't read any of your post but I did notice that you're assigning rather than comparing which makes your first condition always true:

$form['#options'] = 'Please create Role'

should be

$form['#options'] == 'Please create Role'

OK I know where  the problem is I am just not sure why it failing or how to fix it. If I change the if statement like this:

if ($form_state['values']['usergroup'] == 0) {
        //&& $form['#options'] == '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 or administrator".');
        dsm($form);
        dsm($form_state);
}

It fails as it should (this with no roles created so it displaying 'Please create Role'). But if I change it to:

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);
}

then it doesn't fail and validates so there is something wrong with this portion, I run it like this and create images of the dsm outputs so you can see the values are there.

I had to use the following to get it to fail validating and could grab values from dsm.

if ($form_state['values']['usergroup'] == 0) {
        //&& $form['#options'] == '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 or administrator".');
        dsm($form);
        dsm($form_state);
}

 

[attachment deleted by admin]

If I switch it to

$form['#options'] == 'Please create Role'

It will allow both test roles to pass (testrole1 & testrole2), but if I go delete the test roles it also allows 'Please create Role' to pass as well.

 

Try that:

 

if ($form['#options'] == 'Please create Role' || $form_state['values']['usergroup'] == 0) {

I was able to determine it was the $form['#options'] causing the problem, changed this to the value it should have been and I was able to get an if statement that worked and no longer needed the if && a simple if would suffice, but if I had to it would probably look something like this:

if ($form['settings']['usergroup']['#options'][0] == '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".');
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.