drupal-7hook-form-alterdrupal-form-validation

Stumped on Drupal 7 - adding user_profile_form #validation callback via hook_form_alter


I've searched, read, and re-read. I've even stripped a test module down to the most basic elements.

I think this code should work, but the validation callback is never called. I add the callback to the form, but it is gone at the next steps.

Ultimately, my goals is to "validate" submitted usernames, in order to meet a specific business objective. I'm getting stuck just adding the form validation callback, but I'm confident that once that hurdle is overcome, the rest of the functionality is straight forward.

Can someone smarter than me please point me in the right direction to properly add a validation callback?

<?php
/*
    Implements hook_form_alter();
*/
function mymod_form_alter($form, $form_state, $form_id) {
    // catch the user profile form.
    if('user_profile_form' == $form_id) {
        // set debug message
        drupal_set_message(t('adding validation callback in hook_form_alter()...'), 'warning');
        // add a validation callback
        $form['#validate'][] = 'mymod_user_profile_form_validate';
    }
}

/*
    Implements hook_form_<form_id>_alter();
    If mymod_form_alter() was successful, I wouldn't need this hook, it's just here for verification.
*/
function mymod_form_user_profile_form_alter($form, $form_state, $form_id) {
    // check to see if our validation callback is present
    if(!in_array('mymod_user_profile_form_validate', $form['#validate'])) {
        // our validatiation callback is not present
        drupal_set_message(t('The validation callback is missing from #validate in hook_form_[form_id]_alter()'), 'error');
        // since it's not there, try re-adding it?
        $form['#validate'][] = 'mymod_user_profile_form_validate';
    } else {
        // We should see this message, but don't.
        drupal_set_message(t('The validation callback exists!'), 'status');
    }
}

/*
    ?? Implements hook_form_validate(); (or not...) ??
*/
function mymod_user_profile_form_validate($form, $form_state) {
    // why is mymod_user_profile_form_validate() never called??
    drupal_set_message(t('Validation callback called! Whoopee! Success!'), 'status'); // This never happens!

    // if this was working, we would do a bit of logic here on the username.  
    //for this test, let's assume we want to return an error on the username field.
    form_set_error('name', t('Blah, blah, blah, something about your username...'));
}

Solution

  • The variables $form and $form_state should be passed by reference so that your hook function can actually modify them (otherwise the function just get a copy).

    You need to use & (ampersand) symbol added before variable argument in the function signature :

    function mymod_form_alter(&$form, &$form_state, $form_id) {
      # $form is referenced from the global scope using '&' symbol
      $form['#validate'][] = 'mymod_user_profile_form_validate';
    }