Drupal: How to differentiate between new user registration and user password update in hook_user validate operation?
I am using hook_user validate operation 开发者_如何学Cto validate user registration info against my business logic.
I want separate logics to run on registration and change password.
But I am unable to differentiate between the two - both pass through validation and same code is run for both.
How can I differentiate between the two inside the validate op in hook_user?
with $form_id
if ( ($form_id == 'user_profile_form' && arg(3) == NULL) {
// validation code for updating
}
elseif ($form_id == 'user_register') ) {
// validation code for registering
}
In Drupal 7 you may try something like:
/**
* Implements hook_form_FORM_ID_alter().
* Form ID: user_profile_form
*/
function foo_form_user_profile_form_alter($form, &$form_state) {
// Set a custom form validate and submit handlers.
$form['#validate'][] = 'foo_form_user_profile_form_validate';
$form['#submit'][] = 'foo_form_user_profile_form_submit';
}
/**
* Implements hook_form_FORM_ID_alter().
* Form ID: user_register_form
*/
function foo_form_user_register_form_alter($form, &$form_state) {
if ($form['#user_category'] == 'account') {
// Set a custom form validate and submit handlers.
$form['#validate'][] = 'foo_form_user_register_validate';
$form['#submit'][] = 'foo_form_user_register_submit';
}
}
精彩评论