You are here

function webform_validate_date in Webform 6.2

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

Element validation for Webform date fields.

1 string reference to 'webform_validate_date'
_webform_render_date in components/date.inc
Build a form item array containing all the properties of this component.

File

components/date.inc, line 211
Webform module date component.

Code

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

  // Check if the user filled the required fields.
  foreach (array(
    'day',
    'month',
    'year',
  ) as $field_type) {
    if (!is_numeric($form_item[$field_type]['#value']) && $form_item['#required']) {
      form_set_error($form_key, t('!name field is required.', array(
        '!name' => $name,
      )));
      return;
    }
  }

  // Check for a valid date.
  if ($form_item['month']['#value'] !== '' || $form_item['day']['#value'] !== '' || $form_item['year']['#value'] !== '') {
    if (!checkdate((int) $form_item['month']['#value'], (int) $form_item['day']['#value'], (int) $form_item['year']['#value'])) {
      form_set_error($form_key, t('Entered !name is not a valid date.', array(
        '!name' => $name,
      )));
      return;
    }
  }

  // Check the date is between allowed years.
  if ($form_item['year']['#value'] !== '' && is_numeric($component['extra']['year_start']) && is_numeric($component['extra']['year_end'])) {
    if ($form_item['year']['#value'] < $component['extra']['year_start'] || $form_item['year']['#value'] > $component['extra']['year_end']) {
      form_set_error($form_key . '][year', t('The entered date needs to be between the years @start and @end.', array(
        '@start' => $component['extra']['year_start'],
        '@end' => $component['extra']['year_end'],
      )));
      return;
    }
  }
}