Hi all,

A quick one hopefully, im trying to add a custom validation handler to the user login form. i've tried hook_form_FORM_ID_alter and drupal picks it up (as you would expect), but it doesn't do the validation as passed into $form['#validate'][].

So, i've tried using hook_form_alter() and checking for form id and acting on that, with the same result.

/**
 * Implementation of hook_form_alter().
 */
function mmi_user_core_form_alter(&$form, $form_state, $form_id) {
 if ($form_id == 'user_login') {
  array_unshift($form['#validate'], 'mmi_user_core_login_form_validate');
 }
}

Any ideas? it doesn't even step into the validation function, let alone perform any.

I hope its just me going mad here... but always worth a check :)

Comments

deruitern’s picture

Can you try the following code out and see if it works for you?

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login' || 'user_login_block') {
    array_unshift($form['#validate'],'mymodule_user_login_form_validate');
    drupal_set_message('mymodule_form_alter() called', 'status');
  }
}

function mymodule_user_login_form_validate(&$form, &$form_state) {
  drupal_set_message('mymodule_user_login_form_validate() called', 'status');
}
N1ghteyes’s picture

No joy i'm afraid :(

Originally, i tried to do as above, but i got no drupal_set_message output, so i switched to using error_log, just because i cant watch it in terminal in real time, still no joy there.

with the above, i'm still getting no drupal_set_message() output, and definitely no validation happening.

including the validation function, i have this:

/**
 * Implementation of hook_form_alter().
 */
function mmi_user_core_form_alter(&$form, $form_state, $form_id) {
 if ($form_id == 'user_login') {
  array_unshift($form['#validate'], 'mmi_user_core_login_form_validate');
  drupal_set_message('mymodule_form_alter() called', 'status');
 }
}

/**
* Function used to validate the user has access to the system when logging in. (i.e. they have changed corps, changed api settings etc)
*/
function mmi_user_core_login_form_validate(&$form, $form_state){
    drupal_set_message('mymodule_user_login_form_validate() called', 'status');
    $username = check_plain($form_state['values']['name']);
    $userdata = mmi_user_core_get_user_by_name($username);
    $userapidata = mmi_api_core_load_account_array($userdata['uid']);
    
    $validateres = mmi_user_core_user_system_acess_validate_check($userapidata['api_id'], $userapidata['verification_code']); //check we have the required access
    if(isset($validateres)){ //if anything is set here, we have an error
        form_set_error('name', $validateres); //fail validation, output the problem
    }
}

Its not getting into the validation function, and i get no dsm from hook_form_alter, though error_log seemed to work before. As for not having the login block form id, im using the normal login form :) the block is disabled.

so far as i can tell, the above should work... still no joy though :(

deruitern’s picture

Make sure with the code I posted, you do a find and replace of "mymodule" with the name of your module. I apologize for not specifying that initially.

My only other suggestion would be to double check and make sure the module is enabled and maybe clear caches.

I will also mention that your code will only run on the user login page and not the user login block.

N1ghteyes’s picture

all this i know....

the only bits saying mymodule that i left in was the dsm, since thats just text output.

The login block, as i said above is disabled, so no problems there (i'm using the login form). The module is enabled, since i'm trying to validate couple of fields that it creates in the reg form (and checks against an external api on login), and as always caches are clear :)

This is the problem. so far as i can tell it should work :/

deruitern’s picture

When I take the code that you posted and rename the functions to use the name of my module, I see the messages.

That means, like you suspect, the code should work. Sorry I don't have anymore ideas to try, but I will post back if I think of anything else.

N1ghteyes’s picture

well, at least its not the code thats the problem then :P.. well this bit of it anyway.

Thanks

N1ghteyes’s picture

Hi All,

Incase anyone else is having issues, it seems you need to flush cache at least 5 times.... as apparently 4 times weren't enough!

Odd, but its working now so marking this as solved