function ViewsFormAjaxHelperTrait::addLimitedValidation

Limits validation errors for a non-JavaScript fallback submit button.

Parameters

array $element: The form element render array.

\Drupal\Core\Form\FormStateInterface $formState: The current state of the form.

Return value

array Render array.

File

core/modules/views/src/ViewsFormAjaxHelperTrait.php, line 145

Class

ViewsFormAjaxHelperTrait
Provides reusable code to be shared by Views Ajax forms.

Namespace

Drupal\views

Code

public static function addLimitedValidation(array $element, FormStateInterface $formState) : array {
  // Retrieve the AJAX triggering element so we can determine its parents. (We
  // know it's at the same level of the complete form array as the
  // submit-button, so all we have to do to find it is swap out the
  // submit-button's last array parent.)
  $arrayParents = $element['#array_parents'];
  array_pop($arrayParents);
  $arrayParents[] = $element['#views_ui_ajax_data']['trigger_key'];
  $ajaxTriggeringElement = NestedArray::getValue($formState->getCompleteForm(), $arrayParents);
  // Limit this button's validation to the AJAX triggering element, so it can
  // update the form for that change without requiring that the rest of the
  // form be already filled out properly.
  $element['#limit_validation_errors'] = [
    $ajaxTriggeringElement['#parents'],
  ];
  // If we are in the process of a form submission and this is the button that
  // was clicked, the form API workflow in
  // \Drupal::formBuilder()->doBuildForm() will have already copied it to
  // $formState->getTriggeringElement() before our #process function is run.
  // So we need to make the same modifications in $formState as we did to the
  // element itself to ensure that #limit_validation_errors will actually be
  // set in the correct place.
  $clickedButton =& $formState->getTriggeringElement();
  if ($clickedButton && $clickedButton['#name'] == $element['#name'] && $clickedButton['#value'] == $element['#value']) {
    $clickedButton['#limit_validation_errors'] = $element['#limit_validation_errors'];
  }
  return $element;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.