You are here

function timefield_time_validate in Timefield 7

Element validation function

1 string reference to 'timefield_time_validate'
timefield_field_widget_form in ./timefield.module
Implements hook_field_widget_form().

File

./timefield.module, line 548
Defines a Field API field for time

Code

function timefield_time_validate($element, &$form_state, $form) {
  $delta = $element['#delta'];
  $field_name = $element['#field_name'];
  if ($field_value = drupal_array_get_nested_value($form_state['values'], $element['#field_parents'])) {
    $field_parent = drupal_array_get_nested_value($form, $element['#field_parents']);
    if (isset($field_parent[$field_name])) {
      $field = $field_parent[$field_name][$element['#language']][$delta];
    }
    else {
      $field = $field_parent['#field'];
    }
    $values = $field_value[$field_name][$element['#language']][$delta];
    $new_values = array();

    // If empty, set to null.
    if (strlen($values['value']) == 0) {
      if ($field && isset($field['#required']) && $field['#required']) {
        form_error($element['value'], t('!name field is required.', array(
          '!name' => check_plain($element['#title']),
        )));
      }
      form_set_value($element, array(
        'value' => NULL,
      ), $form_state);
      return;
    }
    $date_value = date_parse($values['value']);
    if ($date_value['error_count']) {
      form_error($element['value'], t('The time is not in a format that I understand.'));
    }
    else {
      $parsed_value = timefield_time_to_integer($values['value']);
      $new_values['value'] = $parsed_value;
    }
    if (!empty($values['value2'])) {
      $date_value2 = date_parse($values['value2']);
      if ($date_value2['error_count']) {
        form_error($element['value2'], t('The to time is not in a format that I understand.'));
      }
      else {
        $parsed_value = timefield_time_to_integer($values['value2']);
        if ($values['value'] > $parsed_value || $parsed_value == 0) {
          $parsed_value += 86400;
        }
        $new_values['value2'] = $parsed_value;
      }
    }
    if (!empty($values['days'])) {
      foreach ($values['days'] as $index => $day) {
        $new_values[$index] = $day === 0 ? $day : 1;
      }
    }
    if (!empty($values['label'])) {
      $new_values['label'] = isset($values['label']) ? $values['label'] : '';
    }
    form_set_value($element, $new_values, $form_state);
  }
}