Community

One Page + Multiple Forms + Same Form element name = form_set_error issue

Hello,
I created one custom page where i want to display log-in and registration form.
What i did is, i create one function which called from Menu hook as "drupal_get_form"

$data[] = drupal_get_form('user_login');
$data[] = drupal_get_form('user_register_form');
return $data;

Now once i submit (either log-in or registration) the form without entering any data, its show form_set_error on both forms "username" text field.
I know both form element name for username textfield is same as "name" but it should not highlight both form element. It should be highlighted submitted form textfield only.

I want to highlight only submitted textfield.

Any help ??

Comments

Any one ?

Do any one tried on this ? Any suggestion ?

Unfortunately, the

Unfortunately, the form_set_error() method which statically collects errors when a form is being validated has no internal way to know which form it is that these errors belong to. Hence, it highlights fields from different forms with the same name. You can make the Name field unique for each form by altering them and adding a #parents attribute with a unique parent name for each.

Eg.

<?php
 
// From inside hook_form_user_login_alter().
 
$form['name']['#parents'] = array('user_login', 'name');
 
// From inside hook_form_user_register_alter().
 
$form['name']['#parents'] = array('user_register', 'name');
?>

Caveat: I haven't tried this out myself.

You might also find this answer - http://drupal.stackexchange.com/questions/9814/multiple-field-widget-ins... relevant and helpful.

Edit: Actually, the solution above might not work unless you change the existing form validation/submit functions in the user module as well. I'll report back with a better solution after looking into this a bit more.

Amar