You are here

function date_selector_validate in Date 5

Validation function for date selector $params = an array of values including: required = is a valid date required, default is true opt_fields = an array of fields that need not be filled out, default is empty array

File

./date.inc, line 1067
Date/time API functions

Code

function date_selector_validate($value, $fieldname, $params = array()) {
  $type = $params['type'] > '' ? $params['type'] : DATE_ISO;
  $required = isset($params['required']) ? $params['required'] : 1;
  $opt_fields = is_array($params['opt_fields']) ? $params['opt_fields'] : array();
  $granularity = $params['granularity'] ? $params['granularity'] : array(
    'M',
    'D',
    'Y',
  );
  if ($value['ampm'] == 'pm' && $value['hours'] < 12) {
    $value['hours'] += 12;
  }

  // check for invalid numbers in fields that were submitted using textfields
  if (($required || $value['year']) && !in_array($value['year'], $opt_fields)) {
    if (!date_part_is_valid($value['year'], 'year')) {
      form_set_error($fieldname, t('year must be a number between %min and %max.', array(
        '%min' => DATE_MIN_YEAR,
        '%max' => DATE_MAX_YEAR,
      )));
    }
  }
  if (($required || $value['mon']) && !in_array($value['mon'], $opt_fields)) {
    if (!date_part_is_valid($value['mon'], 'mon')) {
      form_set_error($fieldname, t('month must be a number between 1 and 12.'));
    }
  }
  if (($required || $value['mday']) && !in_array($value['mday'], $opt_fields)) {
    if (!date_part_is_valid($value['mday'], 'day')) {
      form_set_error($fieldname, t('day must be a number between 1 and 31.'));
    }
  }

  // try to construct a date from the submitted values
  if ($required && !date_selector_make_dbdate($value, $type)) {
    form_set_error($fieldname, t('Date cannot be constructed using values %s.', array(
      '%s' => implode(', ', $value),
    )));
    return FALSE;
  }
  else {
    return TRUE;
  }
}