I have a form with a nested subform that has an ajax submit button in it. The subform is built inside a form_alter and attached to the form. The submit buttons #name
is made unique by using a UUID generator.
When I press the ajax submit button, Drupal steps through its form builder steps rebuilding the form. Because of this, the same code in the form_alter is called rebuilding the same subform. The problem is that when it is rebuilt, the submit button gets a brand new #name via the UUID generator. As such, Drupal cannot match the _triggering_element
to a #name
, and the ajax fails.
One solution I thought to try was to attach the #name
to $form_state storage when the form is first built hoping that it would be available when the form is rebuilt. For e.g. in the same form_alter I execute:
$input = $form_state->getUserInput();
if($input){
//This only runs when the form is being rebuilt
}
And I may be able to grab the correct #name
out of $form_state storage and set it so Drupal can correctly match and find the right ajax callbacks.
Is something like this possible? Is there a better way to handle this?
#name
to cause issues. Better to leave that one alone. Not sure what you're trying to do though, so can't really help with a solution.