I am working on a module to add a little validation to Drupal Commerce's add-to-cart form. Here's what I got so far, I've used form_alter to catch the form. Then I add a function to the validation of the form objet, like so:

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

Then in my module I have this code, just to see if the validation is executing:

   function quantity_multiples_checkQuantity_validate($form, &$form_state){
    form_set_error('', t('AN ERROR HAS OCCURRED'));
  }

To me it seems that whenever I add an item to the cart, it should spit out an error on the screen "AN ERROR HAS OCCURRED", but I get nothing. I also what to print_r the $form object to see what I'm working with and I get no luck.

Comments

kenuck’s picture

have you cleared your cache?try drupal_set_message(t('AN ERROR .... ')) in your validation function for your test.

Also confirm that you're form_alter function is being called.

cheers

cday119’s picture

I cleared the cache, changed it to:

drupal_set_message('VALIDATE');

and the form_alter is getting called, I can do print_r within the form alter function on the $form object which gives me a list of add_to_cart forms on that page. I also check this object to make sure 'quantity_multiples_checkQuantity_validate' is part of $form['#validate'] and it is.

michel.settembrino’s picture

I add the solution here for the others having the same issue and getting this page in Google.

See https://www.drupal.org/node/1292742
Solution: take the form and form state by reference (&$form, &$form_state).

vikrama1k1’s picture

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

function quantity_multiples_checkQuantity_validate(&$form, &$form_state){
form_set_error('', t('AN ERROR HAS OCCURRED'));
}