You are here

function webform_validate_date in Webform 5

Same name and namespace in other branches
  1. 5.2 components/date.inc \webform_validate_date()
  2. 6.3 components/date.inc \webform_validate_date()
  3. 6.2 components/date.inc \webform_validate_date()
  4. 7.4 components/date.inc \webform_validate_date()
  5. 7.3 components/date.inc \webform_validate_date()

File

components/date.inc, line 115

Code

function webform_validate_date($field, $field_name, $component_name, $form_key, $cid, $mandatory) {
  static $complete_dates = array();
  switch ($field_name) {
    case 'month':
      $complete_dates[$cid]['month'] = $field['#value'];
      break;
    case 'day':
      $complete_dates[$cid]['day'] = $field['#value'];
      break;
    case 'year':
      $complete_dates[$cid]['year'] = $field['#value'];
      break;
  }

  // Check if the user filled the required fields.
  if (!is_numeric($field['#value']) && $mandatory) {
    form_set_error($form_key, t("@type %name field required", array(
      '@type' => $component_name,
      '%name' => $field_name,
    )));
    $complete_dates[$cid] = array();
    return false;
  }

  // Check for a valid date.
  if (isset($complete_dates[$cid]['month']) && isset($complete_dates[$cid]['day']) && isset($complete_dates[$cid]['year']) && ($complete_dates[$cid]['month'] !== "" || $complete_dates[$cid]['day'] !== "" || $complete_dates[$cid]['year'] !== "")) {
    if (!checkdate((int) $complete_dates[$cid]['month'], (int) $complete_dates[$cid]['day'], (int) $complete_dates[$cid]['year'])) {
      form_set_error($form_key . '][' . $field_name, t("Entered %name is not a valid date.", array(
        '%name' => $component_name,
      )));
      $complete_dates[$cid] = array();
      return false;
    }
    $complete_dates[$cid] = array();
  }
}