You are here

function _partial_date_element_validate in Partial Date 7

TODO: Validates the date type to stop dates like February 30, 2006.

1 call to _partial_date_element_validate()
partial_date_element_validate in ./partial_date.module
TODO: Validates the date type to stop dates like February 30, 2006.

File

./partial_date.admin.inc, line 1138
Less freq. functions for field administration.

Code

function _partial_date_element_validate($element) {
  if (!empty($element['#required']) && partial_date_field_is_empty($element['#value'], array(
    'type' => $element['#type'],
  ))) {
    form_error($element, t('The %label field is required.', array(
      '%label' => $element['#title'],
    )));
  }
  $day = empty($element['#value']['day']) ? 1 : $element['#value']['day'];
  $month = empty($element['#value']['month']) ? 1 : $element['#value']['month'];
  $year = empty($element['#value']['year']) ? NULL : $element['#value']['year'];
  $months = partial_date_month_matrix($year);
  if (!isset($months[$month - 1])) {
    form_error($element, t('The specified month is invalid.'));
  }
  elseif ($day < 1 || $day > $months[$month - 1]) {
    form_error($element, t('The specified month is invalid.'));
  }
  if (!empty($element['#value']['hour'])) {
    if (!is_numeric($element['#value']['hour']) || $element['#value']['hour'] < 0 || $element['#value']['hour'] > 23) {
      form_error($element, t('The specified time is invalid. Hours must be a number between 0 and 23'));
    }
  }
  if (!empty($element['#value']['minute'])) {
    if (!is_numeric($element['#value']['minute']) || $element['#value']['minute'] < 0 || $element['#value']['minute'] > 59) {
      form_error($element, t('The specified time is invalid. Minutes must be a number between 0 and 59'));
    }
  }
  if (!empty($element['#value']['second'])) {
    if (!is_numeric($element['#value']['second']) || $element['#value']['second'] < 0 || $element['#value']['second'] > 59) {
      form_error($element, t('The specified time is invalid. Seconds must be a number between 0 and 59'));
    }
  }

  // Testing what removing the additional elements does...
  // Getting strange submission values.
  foreach (element_children($element) as $child) {
    unset($element[$child]);
  }

  // We could validate a time according to the daylight savings rules....
  // But this is probably overkill.
}