6

I try to add custom validation or submit function to the user registration form, but nothing works.

Here is the code in /sites/all/themes/mytheme/template.php:

function mytheme_user_register_form_validate (&$form, &$form_state) {}
function mytheme_form_user_register_form_validate (&$form, &$form_state) {}
function mytheme_user_register_validate (&$form, &$form_state) {}
function mytheme_user_register_form_submit (&$form, &$form_state) {}
function mytheme_profile_user_form_validate (&$form, &$form_state) {}

I also put similar code to /sites/all/modules/custom/mymodule/mymodule.module:

function mymodule_user_register_form_validate (&$form, &$form_state) {}
function mymodule_form_user_register_form_validate (&$form, &$form_state) {}
function mymodule_user_register_validate (&$form, &$form_state) {}
function mymodule_user_register_form_submit (&$form, &$form_state) {}
function mymodule_profile_user_form_validate (&$form, &$form_state) {}

None of the above functions are called and I have ran out of ideas. As a proof that something works on this form, here is something that does work:

function mytheme_form_user_register_form_alter(&$form, &$form_state, $form_id) {}

This gets called!! But I need to validate...

By the way, the debugger shows (among other) this when debugging the hook_form_alter:

$form['#validate'][0] = 'user_account_form_validate'
$form['#validate'][1] = 'user_validate_picture'
$form['#validate'][2] = 'user_register_validate'
$form['#validate'][3] = 'profile_user_form_validate'
  • 1
    First, your form functions shouldn't go into your theme, so you'd better use module hooks. For your hooks to be taken into account, you have to clear the classes cache. Did you ? – Countzero Feb 17 '12 at 8:57
  • Yeah, I cleared all caches. It's a bit strange that validation must go to module and not theme, but anyway, module hooks don't work either. – camcam Feb 17 '12 at 9:29
  • 1
    It's not strange : theme is for presentation, module for business logic. You can use the simplest and more general hook for your form : hook_form_alter and then test for your form's name inside the hook. As for the custom function, petiar advice below is fine. – Countzero Feb 17 '12 at 10:40
8

Add this in your hook_form_alter:

$form['#validate'][] = 'your_validation_function';

This way you can define an additional validation functions. Make sure you report all validation errors using the form_set_error() function.

  • It is working! I was just wondering why the "normal" hooks don't get called. Nowhere in the documentation it is written that in order to use the hooks you have to add them manually to form validate array (and added function doesn't need to be strictly a hook, like in the example above) – camcam Feb 17 '12 at 10:38
  • It's the other way around : a validation function is just a standard function, not a hook. Hooks don't have to be added to be usable : you just have to use the correct namespace, which you probably didn't in your examples. – Countzero Feb 17 '12 at 10:49
  • Considering all that was already said (use hook in a module and check form id for proper naming), my example contains one function that should be in the correct namespace: mymodule_user_register_form_validate (&$form, &$form_state) because the form id is "user_register_form". So at least this one should get called, according to documentation – camcam Feb 17 '12 at 11:11
  • @camcam Themes can just implement hooks called from drupal_alter(), which means those hooks whose name ends with "_alter()"; the other hooks cannot be implemented in themes. – kiamlaluno Feb 20 '12 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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