You are here

public function WebformCompositeForm::validateForm in Webform Composite Tools 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

File

src/Form/WebformCompositeForm.php, line 124

Class

WebformCompositeForm
Form handler for the Composite add and edit forms.

Namespace

Drupal\webform_composite\Form

Code

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

  // Check for duplicate keys.
  $keys = [];
  $elements = $form_state
    ->getValue('elements');
  foreach ($elements as $delta => $value) {
    $key = $value['key'] ?? NULL;
    if (isset($keys[$key])) {
      $selector = 'elements][items][' . $delta . '][key';
      $message = $this
        ->t('Duplicate key found. The %key key must only be assigned on one element.', [
        '%key' => $key,
      ]);
      $form_state
        ->setErrorByName($selector, $message);
    }
    $keys[$key] = $key;
    $element = WebformArrayHelper::addPrefix($value);

    /** @var \Drupal\webform\Plugin\WebformElementInterface $element_plugin */
    $element_plugin = $this->elementManager
      ->getElementInstance($element);
    if ($element_plugin
      ->hasProperty('options') && empty($element['#options'])) {
      $t_args = [
        '%title' => $element['#title'],
      ];
      $form_state
        ->setErrorByName('elements][items][' . $delta . '][options', $this
        ->t('Options for %title is required.', $t_args));
    }
  }
}