Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I've tried adding validation with hook_form_alter like so:
$form['#validate'][] = 'quantity_multiples_check_quantity_validate';
I've made sure that it has been added to the add to cart form by doing a print_r on the $form object. It includes this:
[#validate] => Array
(
[0] => commerce_cart_add_to_cart_form_validate
[1] => my_module_check_quantity_validate
)
Then I did this, so it should error every time but the form successfully gets submitted.
function quantity_multiples_check_quantity_validate($form, &$form_state){
form_set_error('quantity', t('You must specify a valid quantity to add to the cart.'));
}
Here is all my code that isn't triggering:
function quantity_multiples_form_alter($form, $form_state, $form_id){
if(isset($form['#attributes']['class'])){
if (in_array('commerce-add-to-cart',$form['#attributes']['class'])){
$form['#validate'][] = 'quantity_multiples_check_quantity_validate';
}
}
}
function quantity_multiples_check_quantity_validate($form, &$form_state){
form_set_error('quantity', t('You must specify a valid quantity to add to the cart.'));
}
Comments
Comment #1
rszrama CreditAttribution: rszrama commentedYour form alter hook should take the form and form state by reference:
&$form, &$form_state
not$form, $form_state