Score:1

States on input type button broken since core update

kn flag

I've updated my Drupal project from 9.5.3 to 9.5.7, but the submission button's #states property stopped working.

$form['actions']['submit'] = [
  '#type' => 'submit',
  '#value' => 'Accéder au paiement',
  '#enabled' => FALSE,
];

$form['actions']['submit']['#states'] = [
  'enabled' => [
    [
      ':input[name="name_1"]' => ['empty' => FALSE],
      ':input[name="name_2"]' => ['empty' => FALSE],
      ':input[name="name_3"]' => ['empty' => FALSE],
      [
        ':input[name="name_4"]' => ['!value' => ''],
      ],
    ],
  ],
];

I rolled back to 9.5.3 and upgrade to each release to find out when it stopped working.

Turns out it's from the 9.5.6 release. On the 9.5.7 release, the button is always enabled, but changing my other inputs toggle the class form-disabled and the disable attribute is not triggered.

How can I fix it?

id flag
The states library changed in 9.5.5 in https://www.drupal.org/node/994360. There are comments at the bottom of that issue that may be helpful to you.
kn flag
Posted a workaround here https://www.drupal.org/project/drupal/issues/994360#comment-15013581
id flag
It would be nice to have this question answered with that information.
Score:1
kn flag

Find a small workaround, you need to change the HTML structure of your button/input submit (done on 9.5.7)

Remove the .js-form-submit (example here with twig)

<input{{ attributes.removeClass('js-form-submit') }} />{{ children }}

Add .js-form-item around your input

$form['actions']['submit'] = [
  '#type' => 'submit',
  '#value' => t('Validate'),
  '#enabled' => FALSE,
  '#prefix' => '<div class="js-form-item">',
  '#suffix' => '</div>',
];

$form['actions']['submit']['#states'] = [
  'enabled' => [
    [
      ':input[name="other_input"]' => ['empty' => FALSE],
    ],
  ],
];

Problably not the cleanest, but for now it's working

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.