You are here

public function ComponentSectionForm::validateForm in Module Builder 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

1 method overrides ComponentSectionForm::validateForm()
ModuleHooksForm::validateForm in src/Form/ModuleHooksForm.php
Form validation handler.

File

src/Form/ComponentSectionForm.php, line 867

Class

ComponentSectionForm
Generic form for entering a section of data for a component.

Namespace

Drupal\module_builder\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $data = $form_state
    ->get('data');

  // EntityForm::submitForm() has already called $form_state->cleanValues().
  $data_values = $form_state
    ->getValue($data
    ->getName());

  // ARGH. Because FormValidator does general validation before element or
  // button validation, we call this BEFORE button-specific validation can do
  // things to clean up things specific to the button's action, such as
  // removing data for a changed variant.
  $this
    ->cleanUpValues($data_values, $data);

  // Clear the properties we use in this form, so we don't merge with what's
  // already there. Note that $data is initially loaded from the component
  // entity when the form is built.
  $component_properties_to_use = $this
    ->getFormComponentProperties($data);
  foreach ($component_properties_to_use as $property_name) {

    // dsm("CLEAR $property_name");
    $data
      ->removeItem($property_name);
  }
  try {
    $data
      ->set($data_values);
  } catch (InvalidInputException $e) {
    $form_state
      ->setError($form, $this
      ->t("There was a problem with the form data."));
  }

  // Validate the data and set any violations as form errors.
  // TODO: we've validating the whole data, some of which doesn't appear on
  // the form -- but there shouldn't be violations outside of the form, since
  // those would have been caught when their form page was saved! In theory!
  $violations = $data
    ->validate();
  foreach ($violations as $address => $violation_messages) {
    $form_address = explode(':', $address);
    $key_exists = NULL;
    $form_element = NestedArray::getValue($form, $form_address, $key_exists);

    // Some form elements group all the deltas of a data item together, such
    // as injected services and textareas. In that case, there is no element
    // for the actual delta, and the error should be set on the parent
    // address.
    if (!$key_exists) {
      array_pop($form_address);

      // dsm($form_address);
      $form_element = NestedArray::getValue($form, $form_address, $key_exists);
    }

    // Filter the violations to those elements shown on this form section.
    // The 2nd element of the address corresponds to the names in the
    // $component_properties_to_use array (since the 1st element is 'module).
    if (!in_array($form_address[1], $component_properties_to_use)) {
      continue;
    }
    foreach ($violation_messages as $violation_message) {
      $form_state
        ->setError($form_element, $violation_message);
    }
  }

  // TODO: not sure we should do this here! it means that element and button
  // validators have an incorrect picture of what is going on!
  // TODO: figure out why values filled in by validation don't make it to the
  // form at this point!
  $form_state
    ->set('data', $data);
}