You are here

function commerce_license_duration_validate in Commerce License 7

#element_validate callback for the commerce_license_duration widget.

1 string reference to 'commerce_license_duration_validate'
commerce_license_field_widget_form in ./commerce_license.module
Implements hook_field_widget_form().

File

./commerce_license.module, line 1118
Provides a framework for selling access to local or remote resources.

Code

function commerce_license_duration_validate($element, &$form_state) {

  // 0 is interpreted as "unlimited".
  $value = array(
    'value' => 0,
  );
  if ($element['mode']['#value'] == 'limited') {
    $duration = $element['duration'];
    $duration_value = trim($duration['value']['#value']);

    // Can't use #required on the value element because it shouldn't validate
    // when the mode is set to 'unlimited'.
    if (empty($duration_value)) {
      form_error($element, t('%name field is required.', array(
        '%name' => $element['#title'],
      )));
      form_set_value($element, $value, $form_state);
      return;
    }

    // Convert value into unix timestamp.
    if (!empty($duration['unit']['#value'])) {
      $duration_value *= $duration['unit']['#value'];
    }
    $value['value'] = intval($duration_value);
  }
  form_set_value($element, $value, $form_state);
}