You are here

public function KeyFormBase::validateForm in Key 8

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

1 call to KeyFormBase::validateForm()
KeyEditForm::validateForm in src/Form/KeyEditForm.php
Form validation handler.
1 method overrides KeyFormBase::validateForm()
KeyEditForm::validateForm in src/Form/KeyEditForm.php
Form validation handler.

File

src/Form/KeyFormBase.php, line 212

Class

KeyFormBase
Base form for key add and edit forms.

Namespace

Drupal\key\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  if (!$form_state
    ->isSubmitted()) {
    return;
  }

  // Make sure each plugin settings field is an array.
  foreach ($this->entity
    ->getPluginTypes() as $type) {
    if (!$form_state
      ->getValue($type . '_settings')) {
      $form_state
        ->setValue($type . '_settings', []);
    }
  }
  $processed_values = [
    'submitted' => NULL,
    'processed_submitted' => NULL,
  ];
  foreach ($this->entity
    ->getPlugins() as $type => $plugin) {
    if ($plugin instanceof KeyPluginFormInterface) {
      $plugin_form_state = $this
        ->createPluginFormState($type, $form_state);

      // Special behavior for the Key Input plugin.
      if ($type == 'key_input') {

        // If the provider accepts a key value.
        if ($this->entity
          ->getKeyProvider()
          ->getPluginDefinition()['key_value']['accepted']) {
          $processed_values = $plugin
            ->processSubmittedKeyValue($plugin_form_state);
        }
      }
      $plugin
        ->validateConfigurationForm($form, $plugin_form_state);
      $form_state
        ->setValue($type . '_settings', $plugin_form_state
        ->getValues());
      $this
        ->moveFormStateErrors($plugin_form_state, $form_state);
      $this
        ->moveFormStateStorage($plugin_form_state, $form_state);
    }
  }

  // Store the submitted and processed key values in form state.
  $key_value_data = $form_state
    ->get('key_value');
  $key_value_data['submitted'] = $processed_values['submitted'];
  $key_value_data['processed_submitted'] = $processed_values['processed_submitted'];
  $form_state
    ->set('key_value', $key_value_data);

  // Allow the Key Type plugin to validate the key value. Use the processed
  // key value if there is one. Otherwise, retrieve the key value using the
  // key provider.
  if (isset($processed_values['processed_submitted'])) {
    $key_value = $processed_values['processed_submitted'];
  }
  else {

    // Create a temporary key entity to retrieve the key value.
    $temp_key = new Key($form_state
      ->getValues(), 'key');
    $key_value = $temp_key
      ->getKeyValue(TRUE);
  }
  $plugin_form_state = $this
    ->createPluginFormState('key_type', $form_state);
  $this->entity
    ->getKeyType()
    ->validateKeyValue($form, $plugin_form_state, $key_value);
  $form_state
    ->setValue('key_type_settings', $plugin_form_state
    ->getValues());
  $this
    ->moveFormStateErrors($plugin_form_state, $form_state);
  $this
    ->moveFormStateStorage($plugin_form_state, $form_state);
}