You are here

function webform_validate_time in Webform 6.2

Same name and namespace in other branches
  1. 5.2 components/time.inc \webform_validate_time()
  2. 5 components/time.inc \webform_validate_time()
  3. 6.3 components/time.inc \webform_validate_time()
  4. 7.4 components/time.inc \webform_validate_time()
  5. 7.3 components/time.inc \webform_validate_time()
1 string reference to 'webform_validate_time'
_webform_render_time in components/time.inc
Build a form item array containing all the properties of this component

File

components/time.inc, line 163
Webform module time component.

Code

function webform_validate_time($form_item, $form_state) {
  $form_key = $form_item['#webform_component']['form_key'];
  $name = $form_item['#webform_component']['name'];

  // Check if the user filled the required fields.
  foreach ($form_item['#webform_component'] == '12-hour' ? array(
    'hour',
    'minute',
    'ampm',
  ) : array(
    'hour',
    'minute',
  ) as $field_type) {
    if (!is_numeric($form_item[$field_type]['#value']) && $form_item['#required']) {
      form_set_error($form_key, t('%field field is required.', array(
        '%field' => $name,
      )));
      return;
    }
  }

  // Check for a valid time.
  if ($form_item['hour']['#value'] !== '' || $form_item['minute']['#value'] !== '' || isset($form_item['ampm']) && $form_item['ampm']['#value'] !== '') {
    if (!is_numeric($form_item['hour']['#value']) || !is_numeric($form_item['minute']['#value']) || isset($form_item['ampm']) && $form_item['ampm']['#value'] === '') {
      form_set_error($form_key, t('Entered %name is not a valid time.', array(
        '%name' => $name,
      )));
      return;
    }
  }
}