You are here

function _webform_required_file in Webform 6.3

Same name and namespace in other branches
  1. 5.2 components/file.inc \_webform_required_file()
  2. 5 components/file.inc \_webform_required_file()
  3. 6.2 components/file.inc \_webform_required_file()

A Form API element validate function.

Fix Drupal core's handling of required file fields.

1 string reference to '_webform_required_file'
_webform_render_file in components/file.inc
Implements _webform_render_component().

File

components/file.inc, line 359
Webform module file component.

Code

function _webform_required_file($element, $form_state) {
  $component = $element['#webform_component'];
  $parents = $element['#array_parents'];
  array_pop($parents);
  $form_key = implode('_', $parents);

  // Do not validate requiredness on back or draft button.
  if (isset($form_state['clicked_button']['#validate']) && empty($form_state['clicked_button']['#validate'])) {
    return;
  }

  // Check if a value is already set in the hidden field.
  $values = $form_state['values'];
  $key = array_shift($parents);
  $found = FALSE;
  while (isset($values[$key])) {
    if (isset($values[$key])) {
      $values = $values[$key];
      $found = TRUE;
    }
    else {
      $found = FALSE;
    }
    $key = array_shift($parents);
  }
  if (!$found || empty($values['_fid']) && empty($values['_old'])) {
    if (empty($_FILES['files']['name'][$form_key]) && $component['mandatory']) {
      form_error($element, t('%field field is required.', array(
        '%field' => $component['name'],
      )));
    }
  }
}