You are here

function uc_coupon_add_form_validate in Ubercart Discount Coupons 5

Same name and namespace in other branches
  1. 6 uc_coupon.admin.inc \uc_coupon_add_form_validate()
  2. 7.3 uc_coupon.admin.inc \uc_coupon_add_form_validate()
  3. 7.2 uc_coupon.admin.inc \uc_coupon_add_form_validate()

Coupon form validate handler.

File

./uc_coupon.module, line 609
Provides discount coupons for Ubercart.

Code

function uc_coupon_add_form_validate($form_id, $form) {

  // check to ensure a unique coupon code
  $name = db_result(db_query("SELECT name FROM {uc_coupons} WHERE code = '%s' AND cid <> %d", strtoupper($form['code']), $form['cid']));
  if ($name) {
    form_set_error('code', t('Coupon code already used by %name.', array(
      '%name' => $name,
    )));
  }
  foreach ($form['products'] as $key => $product) {
    if ($product && !preg_match('/\\[nid:(\\d+)\\]$/', $product)) {
      form_set_error('products][' . $key, t('Products must include the node ID.'));
    }
  }
  foreach ($form['users'] as $key => $user) {
    if ($user && !preg_match('/\\[uid:(\\d+)\\]$/', $user)) {
      form_set_error('users][' . $key, t('User names must include the user ID.'));
    }
  }
  if (!$form['used'] && $form['bulk_generate'] && intval($form['bulk_number']) <= 0) {
    form_set_error('bulk_number', t('You must specify the number of codes to generate.'));
  }
  $valid_from = mktime(0, 0, 0, $form['valid_from']['month'], $form['valid_from']['day'], $form['valid_from']['year']);
  $valid_until = mktime(0, 0, 0, $form['valid_until']['month'], $form['valid_until']['day'], $form['valid_until']['year']);
  if ($valid_from > $valid_until) {
    form_set_error('valid_from', t('The coupon start date must be before the expiration date.'));
  }
}