You are here

public function WebformUiElementFormBase::validateForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_ui/src/Form/WebformUiElementFormBase.php \Drupal\webform_ui\Form\WebformUiElementFormBase::validateForm()

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/webform_ui/src/Form/WebformUiElementFormBase.php, line 368

Class

WebformUiElementFormBase
Provides a base class for webform element webforms.

Namespace

Drupal\webform_ui\Form

Code

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

  // Only validate the submit button.
  $button = $form_state
    ->getTriggeringElement();
  if (empty($button['#_validate_form'])) {
    return;
  }

  // Subform state used for validation and getting the element's properties.
  $subform_state = SubformState::createForSubform($form['properties'], $form, $form_state);

  // Validate configuration form.
  $element_plugin = $this
    ->getWebformElementPlugin();
  $element_plugin
    ->validateConfigurationForm($form, $subform_state);

  // Get element validation errors.
  $element_errors = $subform_state
    ->getErrors();
  foreach ($element_errors as $element_error) {
    $form_state
      ->setErrorByName(NULL, $element_error);
  }

  // Stop validation if the element's properties has any errors.
  if ($subform_state
    ->hasAnyErrors()) {
    return;
  }
  $parent_key = $form_state
    ->getValue('parent_key');
  $key = $form_state
    ->getValue('key');

  // Prefix table row child elements with the table row key.
  if ($this
    ->isNew() && ($parent_prefix = $this
    ->getParentKeyPrefix($parent_key))) {
    $key = $parent_prefix . '_' . $key;
    $form_state
      ->setValue('key', $key);
  }

  // Update key for new and duplicated elements.
  $this->key = $key;

  // Clone webform and add/update the element.
  $webform = clone $this->webform;
  $properties = $element_plugin
    ->getConfigurationFormProperties($form, $subform_state);
  $webform
    ->setElementProperties($key, $properties, $parent_key);

  // Validate elements.
  if ($messages = $this->elementsValidator
    ->validate($webform)) {
    $t_args = [
      ':href' => Url::fromRoute('entity.webform.source_form', [
        'webform' => $webform
          ->id(),
      ])
        ->toString(),
    ];
    $form_state
      ->setErrorByName('elements', $this
      ->t('There has been error validating the elements. You may need to edit the <a href=":href">YAML source</a> to resolve the issue.', $t_args));
    foreach ($messages as $message) {
      $this
        ->messenger()
        ->addError($message);
    }
  }
}