Preface: I'm familiar with adding #validate callbacks and have done so previously. The module I'm writing has an additional form_alter that is supplying a #validate callback to a different form and that is working great. I've cleared my drupal caches multiple times.

Question: Is there any reason why the form 'commerce_registration_information_checkout_form' will not fire my custom validation that I've added via $form['#validate'] in 'hook_form_commerce_checkout_form_registration_alter'?

Background: Using D7, commerce, registration, commerce_registration. I'm hoping for someone to point out some knucklehead error I'm making. Also, I'm fearing that this 'seemingly simple' form alter will be tough.

Code:
regtweaks.module

/**
 * Implementation of hook_FORM_ID_alter().
 * Tweak the commerce registration information form.
 */
function regtweaks_form_commerce_checkout_form_registration_alter (&$form, &$form_state, $form_id) {
  $form['#validate'][] = 'regtweaks_reg_info_email_tweak';
}

/*
* Custom validation function that disables the stringent email requirements of
* the commerce_registration module. The use case is:
* Office Asstant X wants to register a few people to attend a conference.
* X wishes to be the point of contact between the conference organizer and X's
* registrants.
*
* The idea is to jump into the validation process and generate fake email
* addresses for use in any empty email fields.  This should allow the rest of
* the registration system to operate normally, while providing a smoother UX for
* individuals who are registering multiple people and those people don't have
* email addresses.
*/
function regtweaks_reg_info_email_tweak (&$form, &$form_state) {
  dpm('regtweak email validation...please?!'); // <- This should show when I submit the 'commerce_checkout_form_registration', right?
}

Comments

jameswoods’s picture

Still being unable to find a hook_ method to accomplish my needs, I've resorted to (my chagrin) a client side script solution. I'm not thrilled, in fact, nearly shamed, but here it is. I've included a .js file with the following code via drupal_add_js()

// Using the closure to map jQuery to $. 
(function ($) {

  // Store our function as a property of Drupal.behaviors.
  Drupal.behaviors.regTweaksModule = { 
    attach: function (context, settings) {
      // Acquire the email field
      $(".fieldset-wrapper .form-item label:contains('Email')").each(function (index) {
        // Get the current label as a jQuery object
        var emailLabel = $(this);
        emailLabel.html(' Registrant has No Email (check box if this registrant has no email)');
        // Get the email field (text box)
        var emailField = emailLabel.next();
        // Create a checkbox
        var emailOptOutCheck = $('<input type="checkbox">');
        // Add the checkbox to the beginning of the label
        emailLabel.prepend(emailOptOutCheck);
        // Operate on the email field when the checkbox is checked
        emailOptOutCheck.change(function () {
          if (emailOptOutCheck.is(':checked')) {
            // Generate some random numbers for the fauxmain...get it? 'faux' main?
            var fauxPrefix = Math.floor((Math.random()*100000)+1);
            var fauxSuffix = Math.floor((Math.random()*100000)+1);
            emailField.toggle();
            emailField.val('No_Email_Specified_' + fauxPrefix + '@fauxmain_' + fauxSuffix + '.com');
          } else {
            emailField.toggle();
            emailField.val('');
          }   
        }); 
      }); 
    }   
};

}(jQuery));

jpontani’s picture

Status: Active » Fixed

Latest dev has a hook you can call now: hook_commerce_registration_information_checkout_form_alter(). This hook will have two variables passed by reference to it: first will be the form array, the second is the form state.

http://drupalcode.org/project/commerce_registration.git/commitdiff/e978e6a

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

camdarley’s picture

Status: Closed (fixed) » Active

Sorry to re-open this issue but adding #validate callback doesn't work for me:

/**
 * Implements hook_commerce_registration_information_checkout_form_alter()
 *
 */
function registration_test_callback_commerce_registration_information_checkout_form_alter(&$form, &$form_state) {
  $form['#validate'] = array('registration_test_callback_form_commerce_checkout_form_registration_validate');
}

function registration_test_callback_form_commerce_checkout_form_registration_validate(&$form, &$form_state) {
  whatchdog('registration_test_callback', 'callback fired');
}

Have I done it right?

camdarley’s picture

Also, would it be possible to add the $order variable to the hook?

davery’s picture

Version: 7.x-2.0-beta3 » 7.x-2.x-dev

I'm not sure this works, either. Using latest dev.

Below, $form[$el][$el1] is where the form elements are, so i try there:

function mymodule_commerce_registration_information_checkout_form_alter(&$form, &$form_state)
{
  reset($form);
  $el = key($form);
  $el1 = key($form[$el]);
  $form[$el][$el1]['#validate'][] = '_mymodule_validate';
}

But it doesn't fire my validator. Neither does $form['#validate'][] = '_mymodule_validate';

If i use mymodule_form_alter with:

$form['buttons']['continue']['#validate'][] = '_mymodule_validate';

then it works, but if i submit the form with failing values twice, the second time gives an error in commerce_registration/includes/commerce_registration.checkout_pane.inc line 180 even though form_set_error is called in my validator.

davery’s picture

My issue in #6 w/ validator not being called the second time is a permission issue as i cannot duplicate it as admin. Haven't nailed down which perm it is yet though.

davery’s picture

It looks like you have to have "register other accounts" permission to be able to submit a form subject to a validator as described in #6 more than once.

blacklabel_tom’s picture

Status: Active » Closed (works as designed)