You are here

function webform_client_form_prevalidate in Webform 7.4

Validates that the form can still be submitted, saved as draft, or edited.

Because forms may be submitted from cache or the webform changed while the submission is in progress, the conditions to allow the form are re-checked upon form submission.

2 string references to 'webform_client_form_prevalidate'
webform_client_form in ./webform.module
Client form generation function.
webform_client_form_process in ./webform.module
Process function for webform_client_form().

File

./webform.module, line 2958
This module provides a simple way to create forms and questionnaires.

Code

function webform_client_form_prevalidate($form, &$form_state) {

  // Refresh the node in case it changed since the form was build and retrieved
  // from cache.
  $node = $form['#node'] = node_load($form['#node']->nid);
  $finished = $form_state['values']['details']['finished'];

  // Check if the user is allowed to submit based on role. This check is
  // repeated here to ensure the user is still logged in at the time of
  // submission, otherwise a stale form in another window may be allowed.
  // $allowed_role set by reference.
  $allowed_roles = _webform_allowed_roles($node, $allowed_role);

  // Check that the submissions have not exceeded the total submission limit.
  $total_limit_exceeded = FALSE;
  if ($node->webform['total_submit_limit'] != -1 && !$finished) {
    $total_limit_exceeded = webform_submission_total_limit_check($node);
  }

  // Check that the user has not exceeded the submission limit.
  // This usually will only apply to anonymous users when the page cache is
  // enabled, because they may submit the form even if they do not have access.
  $user_limit_exceeded = FALSE;
  if ($node->webform['submit_limit'] != -1 && !$finished) {
    $user_limit_exceeded = webform_submission_user_limit_check($node);
  }

  // Check that the form is still open at time of submission.
  // See https://www.drupal.org/node/2317273
  // Consider the webform closed when it's status is closed AND either there
  // is no submission yet (hence isn't being edited) or the user isn't an admin.
  // Another way to consider this is that the form is open when its status is
  // open OR there is a submission and the user is an admin.
  $closed = empty($node->webform['status']) && (empty($form['#submission']) || !user_access('edit all webform submissions'));

  // Prevent submission by throwing an error.
  if (!$allowed_role || $total_limit_exceeded || $user_limit_exceeded || $closed) {
    theme('webform_view_messages', array(
      'node' => $node,
      'page' => 1,
      'submission_count' => 0,
      'user_limit_exceeded' => $user_limit_exceeded,
      'total_limit_exceeded' => $total_limit_exceeded,
      'allowed_roles' => $allowed_roles,
      'closed' => $closed,
      'cached' => FALSE,
    ));
    form_set_error('', NULL);
  }
}