6 replies [Last post]
pcambra's picture
Offline
Bug Finder
Joined: 01/23/2008
Juice: 251
Was this information Helpful?

Hi

I need to add a required pane in the cart panes.
I've managed to add a pane using hook_cart_pane but even if I put elements as #required => TRUE using FAPI, the validation is ignored, I think that this is because the form is different for each pane.

I have something like this:

<?php
function my_module_cart_pane($items) {
 
$panes[] = array(
   
'id' => 'my_module_cart_pane',
   
'title' => t('Title of pane'),
   
'desc' => t("Description of pane"),
   
'weight' => 6,
   
'enabled' => TRUE,
   
'body' => !is_null($items) ? drupal_get_form('my_module_cart_form', $items) : '',
  );
  return
$panes;
}
?>

And the callback form is like this:

<?php
function my_module_cart_form($items) {
   
$form['field'] = array(
     
'#type' => 'checkboxes',
     
'#title' => t('Title'),
     
'#options' => array(t('Text of checkbox')),
     
'#required' => TRUE,
    );

    return

$form;
}
?>

The required * is shown, but is not validated.

I've used this same code in the hook_checkout_pane and the validation DOES work, how can I achieve the same for hook_cart_pane?

Thanks!

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
Re: How to put a required pane in the cart panes (hook_cart_pane

Hi- Did you get anywhere with this? I noticed that clicking Update validates (like after Remove or changing cart item quantities), but that clicking Checkout does not.. Is this related?
-Bronius

pcambra's picture
Offline
Bug Finder
Joined: 01/23/2008
Juice: 251
Hi No, I didn't, since

Hi

No, I didn't, since checkout is the same form for all checkout panes with only one submit button, is very easy to add extra validation functions.
However, cart pane system is slghtly different, each cart pane is a solo form, so the "Checkout" button in the cart can't validate other forms unless you do some nasty things like duplicate your form elements or instead of using the cart pane system, you alter the cart form directly..

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
Re: Hi No, I didn't, since

Thanks for the validation (ha, get it?).

I was able to add a few elements to my view cart form, but bc they were added as a pane, they aren't visible in my validate function. I am looking to add these extra form fields to the cart form itself to affect whether or not to prevent checkout on minimum weight order.. see attached. Any ideas?

-Bronius

AttachmentSize
ubercart-add-non-items-to-form-for-validation.png 107.08 KB
pcambra's picture
Offline
Bug Finder
Joined: 01/23/2008
Juice: 251
hehe, validated Yeah, sure

hehe, validated Eye-wink

Yeah, sure you can add as many as cart panes as you want, but since they are in a different form, you can't validate them on submit of the cart's main form so there is no possible way, afaik, of making those extra panes required.
What you can do is, instead of using a cart pane, add a hook_form_alter to the cart form so you can actually add elements to the form. Playing with the weights of the elements you could add them below the submit button.

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
I'm on it!
pcambra wrote:

...
instead of using a cart pane, add a hook_form_alter to the cart form so you can actually add elements to the form
....

I spent some time yesterday working on just that. I have only some familiarity with FAPI and none with TAPIR, and I see lots of the same form elements repeated-- devel module kpr() is helping and bewildering all at the same time Smiling

I believe that I need to simply slip in these additional form elements, but I wonder, will they interfere with the checkout process? Would I need to remove them during form validation? I guess I'll find out soon enough if I can untangle the $form structure on the view_cart form.

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
thanks for the laughs

pcambra: Thanks for your input. FAPI rules here, and the normal rules apply. I was able to add this extra delivery vs pickup preference defaulted to that defined in User Profile (core) with something like:

<?php
/**
* Implementation of hook_uc_form_alter()
* Prevent checkout based on min/max weight subtotal of items in cart
*/
function uc_cart_weight_form_alter(&$form, &$form_state, $form_id) {
  switch(
$form_id) {
    case
'uc_cart_view_form':
    case
'uc_cart_checkout_form':
     
// insert form elements for Pickup or Delivery option
     
$form['fulfillment_type'] = _generate_fulfillment_type_form_elements();
     
// assign min delivery weight validation function
     
$form['#validate'][] = 'uc_cart_weight_view_form_validate';
      break;
  }
}

function

_generate_fulfillment_type_form_elements() {
  global
$user;
 
$user = user_load($user->uid);
 
//kpr($user);
 
$element = array(
   
'#type' => 'radios',
   
'#title' => t('Order Fulfillment Type'),
   
'#description' => t('Order Fulfillment Type is based on a preference in your TXI Web Account Profile and can be set per order. Delivery orders are subject to additional weight/shipment minimums.'),
   
'#options' => array(
       
'Delivery' => t('Delivery'),
       
'Pick-up' => t('Pick-up'),
      ),
   
'#required' => TRUE,
   
'#default_value' => check_plain($user->profile_delivery),
   
'#weight' => -1,
  );
  return
$element;
}

...

function

uc_cart_weight_view_form_validate(&$form, &$form_state) {
//  kpr($form);
 
$op = strtolower($form_state['clicked_button']['#value']);
  if (
$op == 'checkout') {
   
$isdelivery = ($form['fulfillment_type']['#value'] == 'Delivery') ? true : false;
   
$values = $form_state['values'];
   
$percent_full = uc_cart_weight_get_last_truck_percent_full();
   
$target_weight_min = variable_get('uc_cart_weight_block_minweight', 500);
    if(
$isdelivery && uc_cart_weight_one_truck_load_full()) {
     
$message = "Your order cannot be fulfilled with a delivery truck only ". $percent_full ." percent full, because it does not yet meet the ". $target_weight_min ." lbs shipment weight per truck minimum. Please add more products before completing this order.";
     
form_set_error('error', $message);
    }
  }
}
?>

I appreciate your hints!