6

I'm adding the user login form to the homepage of my site and through printing it out in my page.tpl.php, the validation is getting lost a bit. The first time I enter in an incorrect username or password, nothing happens, but the second time everything acts as it should. I am confused about why it does not display the first time.

In my page.tpl.php

print drupal_render(drupal_get_form('user_login_block'));

In my template.php

if ($form_id == "user_login_block") {
  $form['links']['#markup'] = ' <a class="user-password" href="/user/password">' . t('Forgot username or password?') . '</a>'; 
  $form['name']['#title'] = t('Username'); // Change text on form
  $form['name']['#attributes'] = array('placeholder' => t('Username'));
  $form['pass']['#title'] = t('Password');
  $form['pass']['#attributes'] = array('placeholder' => t('Password'));
  $form['actions']['submit']['#value'] = t('');
}

My Custom Validate Module

function hook_form_alter(&$form, &$form_state, $form_id){
  if($form_id == 'user_login_block'){
    $form['#validate'][] = '_custom_validation';
  }
}

function _custom_validation(&$form, &$form_state){
  $query = db_select('users', 'u');
  $query->fields('u', array('name'));
  $query->condition('u.name', $form_state['values']['name'], '=');
  $result = $query->execute();
  $name = $result->fetchField();
  if(empty($name)){
    form_set_error('user_name', t('Username or password could not could not be found.'));
  }
}

I also have the module "Email Registration" Installed, not sure if this has anything to do with it also.

Would anyone know anything about this?

2 Answers 2

1

What is the reason for you adding the form in that manner and having custom validation etc. when drupal already gives you this?

For example, why not just use the drupal block system or the context module to place the login block on your homepage within one of the regions?

Another thing is that your custom module should not have

function hook_form_alter(&$form, &$form_state, $form_id)

it should be

function MODULE_NAME_form_alter(&$form, &$form_state, $form_id)

Where you replace MODULE_NAME with your module's name.
Although maybe you only had it that way for this post.

Also, regardless of the method you use to show the user_login_block form, it already has its own validation functions that come with it. It already has these three validation functions:
http://api.drupal.org/api/drupal/modules!user!user.module/function/user_login_name_validate/7
http://api.drupal.org/api/drupal/modules!user!user.module/function/user_login_authenticate_validate/7
http://api.drupal.org/api/drupal/modules!user!user.module/function/user_login_final_validate/7

With the last of those doing (among other things) essentially the same validation your custom function is trying to do.

You can still use the form alter to add your attributes and change titles, etc. In this case though the module weight does not matter. It only matters if two modules are both using hook_form_alter() and are changing the same parts of the form.

0

I'm assuming the code in template.php is supposed to alter the user_login_block form. If so, why not move that code inside your hook_form_alter() function? You may also need to give your custom module a higher weight using hook_install inside yourmodule.install:

/**
 * Implementation of hook_install().
 */
function mymodule_install() {
  // Module weight defaults to zero
  db_query("UPDATE {system} SET weight = 2 WHERE name = 'mymodule'");
}

Or if you're comfortable, edit the dB directly by incrementing the weight by one until you find the proper weight.

9
  • Will adding a higher weight give me precedence over other modules called before my custom module? Oct 18, 2012 at 3:02
  • That's correct; hook_form_alter() is very common, and if any other module you have installed modifies user_login_block, you'll want your module weight to be greater so it runs last. See drupal.org/node/651106 for more information
    – Webdrips
    Oct 18, 2012 at 6:11
  • Hello Webdrips, I gave this suggestion a try, but I had no luck. I think you are on to something though, because it looks as though the message is being created, just not displayed until the second request. Any other thoughts? Oct 18, 2012 at 17:01
  • Couple things to try real quick: 1) grep/search for modules modifying user_login_block and disable all except your module; 2) Check the Drupal log and see if anything jumps out at you after you run your form submit.
    – Webdrips
    Oct 18, 2012 at 19:32
  • Yeah, I tried looking at the log to see if there was any errors or orphaned actions, and none that pertained to the login process or the login form, so I think I am good on that side of things. I like your idea about searching for modules that modify user login block, I will try that now, thanks. Oct 18, 2012 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.