You are here

function webform_invitation_code_validate in Webform Invitation 2.0.x

Same name and namespace in other branches
  1. 8 webform_invitation.module \webform_invitation_code_validate()
  2. 7.2 webform_invitation.module \webform_invitation_code_validate()
  3. 7 webform_invitation.module \webform_invitation_code_validate()

Invitation Code element validation.

See also

webform_invitation_webform_submission_form_alter()

1 string reference to 'webform_invitation_code_validate'
webform_invitation_webform_submission_form_alter in ./webform_invitation.module
Implements hook_webform_submission_form_alter().

File

./webform_invitation.module, line 49
Webform Invitation module.

Code

function webform_invitation_code_validate(&$form, FormStateInterface $form_state) {
  $code = $form_state
    ->getValue('webform_invitation_code');

  // Get code details from DB.
  $query = \Drupal::database()
    ->select('webform_invitation_codes', 'c')
    ->fields('c');
  $query
    ->condition('code', $code);
  $result = $query
    ->execute()
    ->fetch();

  // Set error on element when needed.
  if (empty($result)) {
    $form_state
      ->setErrorByName('webform_invitation_code', t('This code is not valid.'));
  }
  elseif (!empty($result->used)) {

    // Not required, handled by webform unique option.
    $form_state
      ->setErrorByName('webform_invitation_code', t('This code has already been used.'));
  }
}