Hi Ubercart maintainers,

I'm trying to pass a custom form validation function for a quantity field on an add to cart form.

Essentially, my clients do not want to have any user be able to purchase more than 10 products from them, and if they enter more than 10, be prompted with a message that instructs the customer to call the sales department. This would normally be as straightforward as passing a validation function with a message if the form_state quantity is greater than 10, but it seems that Ubercart for Drupal 7 appends the NID for each form ID, making them unique. So, instead of being able to set a validation function for uc_add_to_cart_form as the form ID, as I see it I'd have to write a function for every product, which is pretty unsustainable.

Any idea how I can fetch a global form ID for a product's add to cart form?

Also, none of the contrib modules for this task work in any capacity, so a custom solution is the only way at this point.

Thanks!

Comments

For the record, I built a working prototype that works something like this:

<?php
function modulename_form_alter(&$form, &$form_state, $form_id) {
   
$prodnid = arg(1);
   
$fid = "uc_product_add_to_cart_form_".$prodnid;
  if ((
arg(0) == "node") && ($form_id == $fid)) {
     
$form['#validate'][] = 'modulename_quantity';
  }
}
function
modulename_quantity(&$form_state){
    if (
$form_state['qty']['#value'] >10){
   
$message = "We're sorry, we can only sell a maximum of 10 individual items per order. If you'd like to purchase more than 10, please call us at 1.800.800.8000";
   
$name = "error";
   
form_set_error($name, $message, $limit_validation_errors = NULL);
    }
}
?>

However, passing in the $nid from the arg() function is kinda dirty. Any alternatives come to mind?

Status:Active» Fixed

Use http://api.drupal.org/api/drupal/modules!system!system.api.php/function/...

You should be able to simplify your code to:

<?php
function modulename_form_uc_product_add_to_cart_form_alter(&$form, &$form_state) {
 
$form['#validate'][] = 'modulename_quantity';
}
?>

Thanks!!

Status:Fixed» Closed (fixed)

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