You are here

function commerce_coupon_date_range_start_validate in Commerce Coupon 7.2

Element validate callback: validate start date

1 string reference to 'commerce_coupon_date_range_start_validate'
commerce_coupon_date_evaluate_date_configure in modules/date/commerce_coupon_date.module
Inline conditions configure callback: date range form.

File

modules/date/commerce_coupon_date.module, line 85

Code

function commerce_coupon_date_range_start_validate($element, &$form_state, $form) {
  if (empty($element['#value']['date'])) {
    return;
  }
  $type = array_pop($element['#parents']);
  $parents = $element['#parents'];
  $parents[] = $type == 'start' ? 'end' : 'start';
  $comp_value = drupal_array_get_nested_value($form_state['values'], $parents);
  if (!empty($comp_value['date'])) {

    // Get a timestamp for start date.
    $start_date = new DateTime($element['#value']['date'] . ' ' . $element['#value']['time']);
    $start_ts = date_format($start_date, 'U');

    // Get a timestamp for end date.
    $end_date = new DateTime($comp_value['date'] . ' ' . $comp_value['time']);
    $end_ts = date_format($end_date, 'U');
    if ($start_ts > $end_ts) {
      form_set_error(implode('][', $parents), t('Start date must be less than end date.'));
    }
  }
}