1

I use drupal 6 and ubercart 2. My aim is to make phone field in delivery address required if certain shipping method is selected. I have set up a jquery script that shows a message to the user saying that phone is needed after customer selects the shipping method. Despite of this, some customers don't fill out the phone field. I would like to add a non js check after submission of checkout page. I suppose I would need to write a small module. A need help with the start of the code

<?php
IF 
{[mymethod] = true}
then {
  $form['panes']['delivery']['delivery_phone']['#required'] = true;
}
?>

Thank you very much.

EDIT: according to @Adam Balsam answer I tried to make custom module This is uc_checkout_phone.module

<?php
function uc_checkout_phone_uc_cart_checkout_form_alter(&$form, &$form_state) {
  $form['panes']['delivery']['delivery_phone']['#element_validate'] = array('uc_checkout_phone_phone_validate');
}
function uc_checkout_phone_phone_validate($element, &$form_state) {
  if (($form_state['panes']['shipping']['shipping_method']['#value'] == 'Doprava - kurier DPD') && ($element['#value'] == '')) {
   form_error($element, t('You must enter a phone number if you select XXX as a shipping method.'));
 dpm($form_state); }
}
?>

This is uc_checkout_phone.info

name = uc_checkout_phone
description = phone required
core = 6.x

The form id should be uc-cart-checkout-form. I had to write it using underscores. The shipping method is Doprava - kuriér DPD, I tried also the value which is flatrate_3---0 but I get no warning message if I let the phone number empty. Where can be the problem? I named themodule: uc_checkout_phone

  • 1
    Try dpming $form_state in uc_checkout_phone_phone_validate() to ensure your checking the right variable against the right value. – Andy Feb 8 '13 at 20:21
  • @Andy I put dpm($form_state); inside the second function (as updated in question) but I can not see any messages from devel. I view checkout page as admin. – loparr Feb 9 '13 at 22:38
  • The dpm() should be outside the conditional. – Andy Feb 10 '13 at 17:38
  • @Andy I put it outside conditional but no messages again. I tried to put dpm() in the first function as well and I got displayed only an empty wrapper for message with no message inside. – loparr Feb 10 '13 at 19:36
  • 1
    The dpm() should only fire on submission; are you submitting the form? You can use a die() in the validator to ensure that it's being called. If it is being called, and you still have problems seeing the dpm(), you can try using a dd() instead. – Andy Feb 11 '13 at 14:22
1

You will have two functions in your custom module. The first one is hook_form_FORM_ID_alter:

/**
 * This function adds an additional custom validation function to the delivery phone field
 *
 * Make sure you update MYMODULE and YOURFORMID to match your circumstances.
 */
function MYMODULE_form_YOURFORMID_alter(&$form, &$form_state) {
  $form['panes']['delivery']['delivery_phone']['#element_validate'] = array('MYMODULE_phone_validate');
}

The second function is the custom validation function that we added using hook_form_FORM_ID_alter above:

/**
 * Make sure you update MYMODULE again and update the array to match your shipping method
 */
function MYMODULE_phone_validate($element, &$form_state) {
  if (($form_state['panes']['shipping']['shipping_method']['#value'] == 'THE SHIPPING METHOD') && ($element['#value'] == '')) {
    form_error($element, t('You must enter a phone number if you select XXX as a shipping method.'));
  }
}
  • Thank you for reply. I could not make it work. Please see the updated question. – loparr Feb 8 '13 at 19:07
  • I can't provide any more help than what Andy has provided above. You might want to look into creating a simple module using the example module (drupal.org/project/examples) to help you understand some of the basic concepts first. – Adam Balsam Feb 11 '13 at 16:20
  • I updated my question and added code for the custom module. Please review it if there are any mistakes. – loparr Feb 11 '13 at 19:48
0

Rules module is the easiest answer:

The rules module allows site administrators to define conditionally executed actions based on occurring events (known as reactive or ECA rules). It's a replacement with more features for the trigger module in core.

  • In drupal 6 I can only use conditional actions where there is no check if phone number is empty or not. Or did you mean to write custom php inside CA? Can you elaborate a little bit please? – loparr Feb 11 '13 at 22:25
  • well in D6 I guess you cant have entities too, probably wont work for you, but give it a try, with some custom php you may get what you want. with rules you can state a trigger if Y (a condition custom php to see if your field has value or not) is valid DO x. – Sohail Feb 12 '13 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.