You are here

public function WebformCivicrmPostProcess::validate in Webform CiviCRM Integration 8.5

Called after a webform is submitted Or, for a multipage form, called after each page

_state (reference)

Parameters

array $form:

Overrides WebformCivicrmPostProcessInterface::validate

File

src/WebformCivicrmPostProcess.php, line 99
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

public function validate($form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
  $this->form = $form;
  $this->form_state = $form_state;
  $this->rawValues = $form_state
    ->getValues();
  $utils = \Drupal::service('webform_civicrm.utils');
  $this->crmValues = $utils
    ->wf_crm_enabled_fields($webform_submission
    ->getWebform(), $this->rawValues);

  // Even though this object is destroyed between page submissions, this trick allows us to persist some data - see below
  $this->ent = $form_state
    ->get([
    'civicrm',
    'ent',
  ]) ?: [];
  $errors = $this->form_state
    ->getErrors();
  foreach ($errors as $key => $error) {
    $pieces = $utils
      ->wf_crm_explode_key(substr($key, strrpos($key, '][') + 2));
    if ($pieces) {
      list(, $c, $ent, $n, $table, $name) = $pieces;
      if ($this
        ->isFieldHiddenByExistingContactSettings($ent, $c, $table, $n, $name)) {
        $this
          ->unsetError($key);
      }
      elseif ($table === 'address' && !empty($this->crmValues["civicrm_{$c}_contact_{$n}_address_master_id"])) {
        $master_id = $this->crmValues["civicrm_{$c}_contact_{$n}_address_master_id"];

        // If widget is checkboxes, need to filter the array
        if (!is_array($master_id) || array_filter($master_id)) {
          $this
            ->unsetError($key);
        }
      }
    }
  }
  $this
    ->validateThisPage($this->form);
  if (!empty($this->data['participant']) && !empty($this->data['participant_reg_type'])) {
    $this
      ->loadMultiPageData();
    $this
      ->validateParticipants();
  }

  // Process live contribution. If the transaction is unsuccessful it will trigger a form validation error.
  $contribution_enabled = wf_crm_aval($this->data, 'contribution:1:contribution:1:enable_contribution');
  if ($contribution_enabled) {

    // Ensure contribution js is still loaded if the form has to refresh
    $this
      ->addPaymentJs();
    $this
      ->loadMultiPageData();
    if ($this
      ->tallyLineItems()) {
      if ($this
        ->isLivePaymentProcessor() && $this
        ->isPaymentPage() && !$form_state
        ->getErrors()) {
        if ($this
          ->validateBillingFields()) {
          if ($this
            ->createBillingContact()) {
            $this
              ->submitLivePayment();
          }
        }
      }
    }
  }

  // Even though this object is destroyed between page submissions, this trick allows us to persist some data - see above
  $form_state
    ->set([
    'civicrm',
    'ent',
  ], $this->ent);
  $form_state
    ->set([
    'civicrm',
    'line_items',
  ], $this->line_items);
}