Commit 42ed6a5f authored by Rob Phillips's avatar Rob Phillips
Browse files

Issue #3324204 by robphillips: Use #limit_validation_errors to prevent validation errors.

parent 770a6b9b
Loading
Loading
Loading
Loading
+9 −21
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ namespace Drupal\entity_form_steps\Form;

use Drupal\Component\Utility\SortArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;

@@ -103,6 +102,15 @@ class EntityFormSteps {
          static::setAccess($stepName, $form);
        }
      }
      elseif (isset($form['#fieldgroups'][$stepName])) {
        // Limit validation errors to elements on the current step.
        if (!isset($form['actions']['submit']['#limit_validation_errors'])) {
          $form['actions']['submit']['#limit_validation_errors'] = [];
        }
        foreach ($form['#fieldgroups'][$stepName]->children as $key) {
          $form['actions']['submit']['#limit_validation_errors'][] = [$key];
        }
      }
    }
    if ($title = Markup::create($step['format_settings'][$entity->isNew() ? 'add_label' : 'edit_label'])) {
      $form['#title'] = $title;
@@ -206,27 +214,7 @@ class EntityFormSteps {
          static::setAccess($element, $form);
        }
        $form[$element]['#access'] = FALSE;

        // Recursively unset required property on element. Typically, disabling
        // access is enough to limit validation errors. Complex field types
        // like image and media still throw errors with access disabled.
        static::unsetRequired($form[$element]);
      }
    }
  }

  /**
   * Recursively unset required property an element.
   *
   * @param array $element
   *   The element.
   */
  public static function unsetRequired(array &$element): void {
    if (isset($element['#required'])) {
      $element['#required'] = FALSE;
      }
    foreach (Element::children($element) as $child) {
      static::unsetRequired($element[$child]);
    }
  }