You are here

function calendar_systems_date_validate in Calendar Systems 7.3

Same name and namespace in other branches
  1. 8 calendar_systems.module \calendar_systems_date_validate()
  2. 6.3 calendar_systems.module \calendar_systems_date_validate()
  3. 6 calendar_systems.module \calendar_systems_date_validate()
  4. 7 calendar_systems.module \calendar_systems_date_validate()
  5. 7.2 calendar_systems.module \calendar_systems_date_validate()

Element validation callback for date fields.

Parameters

$element: Form element.

$form_state: Known form state array.

1 string reference to 'calendar_systems_date_validate'
calendar_systems_form_alter in ./calendar_systems.module
Implements hook_form_alter().

File

./calendar_systems.module, line 224
Contains Calendar Systems hook implementations and helpers.

Code

function calendar_systems_date_validate($element, &$form_state) {
  if (!empty($element['#value'])) {

    // Get proper calendar instance:
    $calendar = calendar_systems_get_calendar_instance();

    // Extract date and time:
    $date = preg_replace('/([0-9]+\\-[0-9]+\\-[0-9]+) (.*)/si', '$1', $element['#value']);
    $time = preg_replace('/([0-9]+\\-[0-9]+\\-[0-9]+) (.*)/si', '$2', $element['#value']);
    $invalid_date = TRUE;

    // Convert to timestamp:
    if (preg_match('/^([0-9]{2,4})[-\\/\\\\]([0-9]{1,2})[-\\/\\\\]([0-9]{1,2})( +([0-9]{1,2})[:]([0-9]{1,2})[:]([0-9]{1,2}))?/', $date, $regs)) {
      $r = $calendar
        ->isDateValid($regs[2], $regs[3], $regs[1]);
      if ($r !== 0 && $r !== FALSE) {
        $element['#value'] = $calendar
          ->strToTimestamp("{$date} 10:10:10");
        $element['#value'] = date("Y-m-d {$time}", $element['#value']);

        // And set the form value:
        form_set_value($element, $element['#value'], $form_state);
        $invalid_date = FALSE;
      }
    }
    if ($invalid_date) {
      form_set_error($element['#name'], t('You have to specify a valid date.'));
    }
  }
}