[development] Creating multiple login blocks with, different validation rules.

nan wich nan_wich at bellsouth.net
Fri May 14 10:54:07 UTC 2010

Great, Anthony. I suggest that you write up a little snippet page for the Handbooks so it can be found with a search.

BTW, remember the array_unshift technique, because you will need it again, especially if you do any node_form alters. I worked that same one out some time ago and am amazed at how many times I have to get my submit handlers to fire first. I just had to do it again a couple of days ago on the comment_form. So documenting this in the handbooks could save people a lot of time in the future.
Nancy E. Wichmann, PMP
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.

From: Anth <malkouna at yahoo.com.au>
To: development at drupal.org
Sent: Fri, May 14, 2010 2:02:50 AM
Subject: Re: [development] Creating multiple login blocks with, different validation rules.


Thanks for the tips everyone. Worked it out, will describe what I ended up doing in case it's useful to anyone.

Nancy and Earnie, I was using MultiBlock and the core of the problem for me was that they *don't* change the $form_id so there is nothing to differentiate them. Alex, yep, I'd started to look at hook_forms so your tip was a good kick in the right direction. Lee, by the time I got your email I'd worked it out :)

So as an example, I had to throw away MultiBlock and generate the blocks on my own but it was pretty easy to do. My application is a timesheet application and there is a role called 'candidate' that needed a separate login. The only real problems I had after getting the hang of all that is for some reason I had to shift my validation function into the first slot (see comments in hook_form_alter) to get it to fire, and if I do a form_set_error(), because the elements are the same name the red error border appears on both, but hey I can live with that.

in my hook_block:

case 'view':
$block['subject'] = 'Candidate Login';
$block['content'] = drupal_get_form('candidate_login_block');
return $block;

Now because 'candidate_login_block' doesn't exist as a function that can be called by drupal_get_form I want drupal_get_form to be called as drupal_get_form('user_login_block') and to make this happen I set up a hook_forms (note, not hook_form) as below:

function timesheet_forms($form_id, $args) {
$forms['candidate_login_block']['callback'] = 'user_login_block';
return $forms;

This all means that in my hook_form_alter I can do:

switch($form_id) {
case 'candidate_login_block':
// $form['#validate'][] = '_timesheet_candidate_login_validate';
// The line above doesn't seem to result in my validation function getting called so had to use the form below.
array_unshift($form['#validate'], '_timesheet_candidate_login_validate');

In my function _timesheet_candidate_login_validate I can do whatever I want to invalidate the form (I called user_load with the entered field and checked its roles). Done.


On 14/05/2010 3:37 PM, Lee Rowlands wrote:
> For a decent Hook_forms example look to ubercart’s uc_product, it uses hook_forms to register the ‘add to cart’ forms : http://api.lullabot.com/uc_product_forms
> *>* I think the answer about using hook_forms is the way to go, but last I looked the API docs were really bad on that hook.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20100514/9e52c7ae/attachment.html 

More information about the development mailing list