diff --git a/commerce_coupon_ui.module b/commerce_coupon_ui.module index 6565730073179867cfe22e467f50355d93bf5fe1..9a3dee75708210aefdbeee33204dd71026d5a802 100644 --- a/commerce_coupon_ui.module +++ b/commerce_coupon_ui.module @@ -40,9 +40,8 @@ function commerce_coupon_ui_menu() { $items['admin/commerce/coupons/add/' . strtr($type, array('_' => '-'))] = array( 'title' => 'Create !name', 'title arguments' => array('!name' => $coupon_type->label), - 'page callback' => 'drupal_get_form', 'page callback' => 'commerce_coupon_ui_coupon_form_wrapper', - 'page arguments' => array(commerce_coupon_create($type)), + 'page arguments' => array(commerce_coupon_create($type), 'add'), 'access callback' => 'commerce_coupon_access', 'access arguments' => array('create', $type), ); @@ -53,7 +52,7 @@ function commerce_coupon_ui_menu() { $items['admin/commerce/coupons/%commerce_coupon'] = array( 'title' => 'Edit', 'page callback' => 'commerce_coupon_ui_coupon_form_wrapper', - 'page arguments' => array(3), + 'page arguments' => array(3, 'edit'), 'access callback' => 'entity_access', 'access arguments' => array('update', 'commerce_coupon', 3), 'weight' => 0, @@ -247,14 +246,14 @@ function theme_commerce_coupon_ui_add_list($variables) { * @param $coupon * The coupon object being edited by this form. */ -function commerce_coupon_ui_coupon_form_wrapper($coupon) { +function commerce_coupon_ui_coupon_form_wrapper($coupon, $op = NULL) { // Add the breadcrumb for the form's location. commerce_coupon_ui_set_breadcrumb(); // Include the forms file from the Coupon module. module_load_include('inc', 'commerce_coupon', 'includes/commerce_coupon_ui.forms'); - return drupal_get_form('commerce_coupon_form', $coupon); + return drupal_get_form('commerce_coupon_form', $coupon, $op); } /** diff --git a/includes/commerce_coupon_ui.forms.inc b/includes/commerce_coupon_ui.forms.inc index 4a5bd90be165fe587fdc917ef0957b21369631b1..1ffb8555acaba2b3f8ce57ce6017c0f36527aeac 100644 --- a/includes/commerce_coupon_ui.forms.inc +++ b/includes/commerce_coupon_ui.forms.inc @@ -52,12 +52,16 @@ function commerce_coupon_form_validate(&$form, &$form_state) { form_set_error('is_active', t('You can\'t enable a coupon of a coupon type that is disabled')); } - if ($form_state['op'] == 'add') { + if ($form_state['op'] == 'add' || $form_state['op'] == 'edit') { $lang_code = field_language('commerce_coupon', $form_state['commerce_coupon'], 'commerce_coupon_code'); if (isset($form_state['values']['commerce_coupon_code'][$lang_code][0]['value'])) { $coupon_code = $form_state['values']['commerce_coupon_code'][$lang_code][0]['value']; if (!empty($coupon_code) && commerce_coupon_code_exists($coupon_code)) { - form_set_error('commerce_coupon_code][' . $lang_code, t('This coupon code is already in use.')); + // Don't invalidate duplicate code if we are simply updating one. + if ($form_state['op'] == 'add' || (isset($coupon->commerce_coupon_code) && + $coupon->commerce_coupon_code[$lang_code][0]['value'] != $coupon_code)) { + form_set_error('commerce_coupon_code][' . $lang_code, t('This coupon code is already in use.')); + } } } }