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();
Comments
Comment #2
quietone commented