Form does not get rebuild in AJAX update even if #limit_validation_errors removes errors

Created on 27 May 2024, 3 months ago
Updated 17 July 2024, about 2 months ago

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

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
FormΒ  β†’

Last updated 1 minute ago

Created by

πŸ‡¬πŸ‡§United Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024