This is not a bug in this module; it's Core: #1091852: Display Bug when using #states (Forms API) with Ajax Request
I have a multivalue item with two elements that are set to invisible, based on the state of an element outside of the multivalue element:
$form['cert_type'] = [ '#type' => 'select', '#options' => ['file', 'config'], ..... ];
$form['certificates'] = [
'#type' => 'multivalue',
'#title' => $this->t('X.509 Certificate(s)'),
'file' => [
'#type' => 'textfield',
'#title' => $this->t('Certificate Filename'),
'#states' => [
'visible' => [
':input[name="cert_type"]' => ['value' => 'file'],
],
],
],
'cert' => [
'#type' => 'textarea',
'#title' => $this->t('Certificate'),
'#states' => [
'visible' => [
':input[name="cert_type"]' => ['value' => 'config'],
],
],
],
This works fine while rendering the initial form; one of the two alternative inputs in the multivalue elements is hidden.
But after the user clicks "Add another item", all elements in all items are visible... until the user changes the value of the 'type' select element again. Thins do work correctly when #1091852 is applied.
To readers: just be warned about this.
I tried to hack some JS apply the #states just after AJAX reload, in https://git.drupalcode.org/project/samlauth/-/commit/2196d9f024d3ab7e9f1.... And failed.
So for now (until Core gets fixed or a JS expert creates a workaround), I just let the AJAX form element print a message "Apologies if multiple types of input elements are visible in every row. Please fill only the appropriate type, or re-select the "Type" above."
Comments
Comment #2
sardara commentedSince it's a core bug, I'll close the issue.