1 date.field.inc date_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors)

Implements hook_field_validate().

File

core/modules/date/date.field.inc, line 448
Field hooks to implement a date field.

Code

function date_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  $flexible = 0;

  // Don't try to validate if there were any errors before this point
  // since the element won't have been munged back into a date.
  if (!form_get_errors()) {
    foreach ($items as $delta => $item) {
      if (is_array($item) && isset($item['value'])) {
        $process = date_process_values($field, $instance);
        $timezone = !empty($item['timezone']) ? $item['timezone'] : NULL;
        $date1 = new BackdropDateTime($item['value'], $timezone, date_type_format($field['type']));
        if (count($process) == 1 || (empty($item['value2']) && $item['value2'] !== 0)) {
          $date2 = clone($date1);
        }
        else {
          $date2 = new BackdropDateTime($item['value2'], $timezone, date_type_format($field['type']));
        }
        $valid1 = $date1->validGranularity($field['settings']['granularity'], $flexible);
        $valid2 = $date2->validGranularity($field['settings']['granularity'], $flexible);

        foreach ($process as $processed) {
          if ($processed == 'value' && $field['settings']['todate'] && !$valid1 && $valid2) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'value',
              'message' => t("A 'Start date' date is required for field %field #%delta.", array('%delta' => $field['cardinality'] ? intval($delta + 1) : '', '%field' => $instance['label'])),
            );
          }
          if ($processed == 'value2' && $field['settings']['todate'] == 'required' && ($instance['required'] && $valid1 && !$valid2)) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'value2',
              'message' => t("An 'End date' is required for field %field #%delta.", array('%delta' => $field['cardinality'] ? intval($delta + 1) : '', '%field' => $instance['label'])),
            );
          }
        }
      }
    }
  }
}