1 date.class.inc public BackdropDateTime::arrayErrors($arr)

Finds possible errors in an array of date part values.

The forceValid() function will change an invalid value to a valid one, so we just need to see if the value got altered.

Parameters

array $arr: An array of date values, keyed by date part.

Return value

array: An array of error messages, keyed by date part.

File

core/includes/date.class.inc, line 793

Class

BackdropDateTime
Extends PHP DateTime class for use with Backdrop.

Code

public function arrayErrors($arr) {
  $errors = array();
  $now = date_now();
  $default_month = !empty($arr['month']) ? $arr['month'] : $now->format('n');
  $default_year = !empty($arr['year']) ? $arr['year'] : $now->format('Y');

  $this->granularity = array();
  foreach ($arr as $part => $value) {
    // Explicitly set the granularity to the values in the input array.
    if (is_numeric($value)) {
      $this->addGranularity($part);
    }
    // Avoid false errors when a numeric value is input as a string by casting
    // as an integer.
    $value = intval($value);
    if (!empty($value) && $this->forceValid($part, $value, 'now', $default_month, $default_year) != $value) {
      // Use a switch/case to make translation easier by providing a different
      // message for each part.
      switch ($part) {
        case 'year':
          $errors['year'] = t('The year is invalid.');
          break;

        case 'month':
          $errors['month'] = t('The month is invalid.');
          break;

        case 'day':
          $errors['day'] = t('The day is invalid.');
          break;

        case 'hour':
          $errors['hour'] = t('The hour is invalid.');
          break;

        case 'minute':
          $errors['minute'] = t('The minute is invalid.');
          break;

        case 'second':
          $errors['second'] = t('The second is invalid.');
          break;
      }
    }
  }
  if ($this->hasTime()) {
    $this->addGranularity('timezone');
  }
  return $errors;
}