function uc_coupon_add_form_validate in Ubercart Discount Coupons 7.3
Same name and namespace in other branches
- 5 uc_coupon.module \uc_coupon_add_form_validate()
- 6 uc_coupon.admin.inc \uc_coupon_add_form_validate()
- 7.2 uc_coupon.admin.inc \uc_coupon_add_form_validate()
Coupon form validate handler.
File
- ./
uc_coupon.admin.inc, line 846 - Discount Coupons administration pages.
Code
function uc_coupon_add_form_validate($form, &$form_state) {
drupal_add_css(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.css');
drupal_add_js(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.admin.js');
$name = db_query("SELECT name FROM {uc_coupons} WHERE code = :code AND cid <> :cid", array(
':code' => strtoupper($form_state['values']['code']),
':cid' => isset($form['#uc_coupon_cid']) ? $form['#uc_coupon_cid'] : 0,
))
->fetchField();
if ($name) {
form_set_error('code', t('Coupon code already used by %name.', array(
'%name' => $name,
)));
}
if (isset($form['#uc_coupon_used']) && $form['#uc_coupon_used'] && isset($form_state['values']['bulk_number']) && isset($form['#uc_coupon']->data['bulk_number']) && $form_state['values']['bulk_number'] < $form['#uc_coupon']->data['bulk_number']) {
form_set_error('bulk_number', t('You cannot decrease the number of bulk codes for a coupon that has been used.'));
}
if (!preg_match('/^(=(?!.*%))?\\d+(\\.\\d+)?%?$/', $form_state['values']['discount'])) {
form_set_error('discount', t('Invalid discount.'));
}
if (substr($form_state['values']['discount'], 0, 1) == '=' && ($form_state['values']['apply_to'] == 'subtotal' || $form_state['values']['apply_to'] == 'products_total')) {
form_set_error('apply_to', t('Set prices cannot be applied to the order subtotal or matching products total.'));
}
if ($form_state['values']['apply_to'] == 'cheapest' || $form_state['values']['apply_to'] == 'expensive') {
if (!preg_match('/^[1-9]\\d*$/', $form_state['values']['apply_count'])) {
form_set_error('apply_count', t('You must specify the maximum number of products to discount.'));
}
}
foreach ($form_state['values']['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_state['values']['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 ((!isset($form['#uc_coupon_used']) || $form['#uc_coupon_used'] == 0) && $form_state['values']['bulk_generate'] && intval($form_state['values']['bulk_number']) <= 0) {
form_set_error('bulk_number', t('You must specify the number of codes to generate.'));
}
if ($form_state['values']['use_validity']) {
$valid_from = _uc_coupon_mktime($form_state['values']['valid_from']['month'], $form_state['values']['valid_from']['day'], $form_state['values']['valid_from']['year']);
$valid_until = _uc_coupon_mktime($form_state['values']['valid_until']['month'], $form_state['values']['valid_until']['day'], $form_state['values']['valid_until']['year']);
if ($valid_from > $valid_until) {
form_set_error('valid_from', t('The coupon start date must be before the expiration date.'));
}
}
}