Problem/Motivation

In a form AJAX update, the form class's form builder method is called twice. The second time is for the form rebuild, and it's at this point that the form builder can apply logic based on the change the user has just made. For example, if the user just changed a dropdown from 'dogs' to 'cats' which triggers an AJAX call, this is when the form could change another element from showing dogs to showing cats.

However, if the AJAX submission causes form errors, for example because some required elements are not filled in, then the rebuild doesn't happen.

This is the case even if you use #limit_validation_errors. That's because although FormValidator::handleErrorsWithLimitedValidation() removes values, it doesn't clean up FormState::$anyErrors and so this check in FormBuilder fails:

      if (($form_state->isRebuilding() || !$form_state->isExecuted()) && !FormState::hasAnyErrors()) {
        // Form building functions (e.g., self::handleInputElement()) may use
        // $form_state->isRebuilding() to determine if they are running in the
        // context of a rebuild, so ensure it is set.
        $form_state->setRebuild();

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

joachim created an issue. See original summary.

quietone’s picture

Version: 11.0.x-dev » 11.x-dev

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.