You are here

function commerce_coupon_form_validate in Commerce Coupon 7

Same name and namespace in other branches
  1. 7.2 includes/commerce_coupon.admin.inc \commerce_coupon_form_validate()

Form API submit callback for the type form.

File

includes/commerce_coupon_ui.forms.inc, line 47
Commerce Coupon editing UI.

Code

function commerce_coupon_form_validate(&$form, &$form_state) {
  $coupon =& $form_state['commerce_coupon'];
  $coupon_type = commerce_coupon_type_load($coupon->type);

  // Don't allow to enable a coupon from a coupon type non enabled.
  if (empty($coupon_type->status) && $coupon->is_active == 1) {
    form_set_error('is_active', t('You can\'t enable a coupon of a coupon type that is disabled'));
  }
  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)) {

        // 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.'));
        }
      }
    }
  }
  field_attach_form_validate('commerce_coupon', $form_state['commerce_coupon'], $form, $form_state);
}