You are here

function date_field_validate in Date 7

Same name and namespace in other branches
  1. 8 date.field.inc \date_field_validate()
  2. 7.3 date.field.inc \date_field_validate()
  3. 7.2 date.field.inc \date_field_validate()

Implements hook_field_validate().

File

./date.field.inc, line 300
Field hooks to implement a date field.

Code

function date_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  $field_name = $field['field_name'];
  $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) && !empty($item)) {
        $process = date_process_values($field, $instance);
        $date1 = new DateObject($item['value'], $item['timezone'], date_type_format($field['type']));
        if (empty($item['value2']) && $item['value2'] !== 0) {
          $date2 = clone $date1;
        }
        else {
          $date2 = new DateObject($item['value2'], $item['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 'From date' date is required for field %field #%delta.", array(
                '%delta' => $field['cardinality'] ? intval($delta + 1) : '',
                '%field' => t($instance['label']),
              )),
            );
          }
          if ($processed == 'value2' && $field['settings']['todate'] == 'required' && ($instance['required'] && $valid1 && !$valid2)) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'value2',
              'message' => t("A 'To date' is required for field %field #%delta.", array(
                '%delta' => $field['cardinality'] ? intval($delta + 1) : '',
                '%field' => t($instance['label']),
              )),
            );
          }
        }
      }
    }
  }
}