You are here

public function SalesforceMappingFieldsForm::validateForm in Salesforce Suite 8.3

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

modules/salesforce_mapping/src/Form/SalesforceMappingFieldsForm.php, line 230

Class

SalesforceMappingFieldsForm
Salesforce Mapping Fields Form.

Namespace

Drupal\salesforce_mapping\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Transform data from the operations column into the expected schema.
  // Copy the submitted values so we don't run into problems with array
  // indexing while removing delete field mappings.
  $values = $form_state
    ->getValues();
  if (empty($values['field_mappings'])) {

    // No mappings have been added, no validation to be done.
    return;
  }
  $key = $values['key'];
  $key_mapped = FALSE;
  foreach ($values['field_mappings'] as $i => $value) {

    // If a field was deleted, delete it!
    if (!empty($value['ops']['delete'])) {
      $form_state
        ->unsetValue([
        "field_mappings",
        "{$i}",
      ]);
      continue;
    }

    // Pass validation to field plugins before performing mapping validation.
    $field_plugin = $this->entity
      ->getFieldMapping($value);
    $sub_form_state = SubformState::createForSubform($form['field_mappings_wrapper']['field_mappings'][$i], $form, $form_state);
    $field_plugin
      ->validateConfigurationForm($form['field_mappings_wrapper']['field_mappings'][$i], $sub_form_state);

    // Send to drupal field plugin for additional validation.
    if ($field_plugin
      ->config('salesforce_field') == $key) {
      $key_mapped = TRUE;
    }
  }
  if (!empty($key) && !$key_mapped) {

    // Do not allow saving mapping when key field is not mapped.
    $form_state
      ->setErrorByName('key', t('You must add the selected field to the field mapping in order set an Upsert Key.'));
  }
}