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

Anth malkouna at yahoo.com.au
Fri May 14 06:02:50 UTC 2010


Hi,

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');
break;

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.

Thanks,
Anthony.

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.
>


More information about the development mailing list