Had same problem,
I have multiple same forms on one page generated in loop.
I met the problem when I wanted to inject HttpClient to Form but back then was passing customValue through constructor.
My approach to solve this issue was define Form as service and pass custom id via property setter:
.services.yml
services:
my_module.my_form:
class: Drupal\my_module\Form\MyForm
arguments: [ '@http_client' ]
tags:
- { name: form }
CustomClass:
$form_as_service = \Drupal::service('my_module.custom_form');
$form_as_service->setCustomValue($custom_value);
$form = $this->formBuilder->getForm($form_as_service, $additional_parameters);
Form Class:
protected string $customValue;
public function setCustomValue(string $value) {
$this->customValue = $value;
}
public function getFormId() {
return 'base_id_'.This->customValue;
}
Maybe it's not ideal, but I think it could be helpful for others.