I searched the issue queue but didn't see a duplicate. I apologize if I missed one!

D7 core does not seem to pass down #states or #attributes assigned to a parent password_confirm element, to it's children.
I was trying to turn autocomplete off for the password confirm field and add a state.

$form['password_field']['#pre_render'] = array('my_form_process_password_confirm');

function my_form_process_password_confirm($elements) {
  foreach (element_children($elements) as $element) {
    $elements[$element]['#attributes']['autocomplete'] = 'off';
    $elements[$element]['#states'] = array(
      'visible' => array(
      	':input[name="my_checkbox"]' => array('checked' => TRUE),
      ),
    );
  }

return $elements;
}

Steps to reproduce

Create a custom form where password and password confirm are required after selecting another field value e.g.

    $form['autogenerate_password'] = [
      '#title' => $this->t('Autogenerate password'),
      '#type' => 'checkbox',
      '#default_value' => 1,
    ];
    $form['password_confirm'] = [
      '#title' => $this->t('Enter password'),
      '#type' => 'password_confirm',
      '#states' => [
        'visible' => [
          ':input[name="autogenerate_password"]' => ['checked' => FALSE],
        ],
      ],
    ]

On launch the Enter password field will be shown.

Proposed resolution

Add the states data selector to the wrapper attributes

CommentFileSizeAuthor
#25 1427838-nr-bot.txt90 bytesneeds-review-queue-bot

Issue fork drupal-1427838

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Sji Infotech’s picture

hi nice Question
i want also this type questions .

Thankful

norman.lol’s picture

For me a container around the password_confirm did the trick. Fieldset works too.

<?php
$form['user_select'] = array(
  '#type' => 'select',
  '#options' => array('' => '- ' . t('None') . ' -') + _get_users_by_role('Subadministration'),
  '#required' => TRUE,
  '#description' => t('Chose account'),
);

$form['pass_wrapper'] = array(
    '#type' => 'container',
    '#states' => array(
      'invisible' => array(
        ':input[name="user_select"]' => array('value' => ''),
      ),
    ),
  );

$form['pass_wrapper']['confirm_password'] = array(
  '#type' => 'password_confirm',
  '#required' => TRUE,
  '#description' => t('Type same password in both fields'),
);
?>
chi’s picture

Version: 7.x-dev » 8.0.x-dev
Issue summary: View changes

Lets move it forward.

chi’s picture

Title: password_confirm children do not pick up #states or #attributes » password and password_confirm children do not pick up #states or #attributes
mglaman’s picture

Confirming this is still an issue. Had to implement the following while working on the Drupal Commerce order add form for new customer creation.

      $form['customer']['password'] = [
        '#type' => 'container',
      ];
      $form['customer']['password']['generate'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Generate customer password'),
        '#default_value' => 1,
      ];
      // We have to wrap the password_confirm element in order for #states
      // to work proper. See https://www.drupal.org/node/1427838.
      $form['customer']['password']['password_confirm_wrapper'] = [
        '#type' => 'container',
        '#states' => [
          'visible' => [
            ':input[name="generate"]' => ['checked' => FALSE],
          ],
        ],
      ];
      $form['customer']['password']['password_confirm_wrapper']['provided'] = [
        '#type' => 'password_confirm',
        '#size' => 25,
        '#required' => TRUE,
      ];

Update. HTML5 validation prevents this from working.

An invalid form control with name='pass[pass1]' is not focusable.
An invalid form control with name='pass[pass2]' is not focusable.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

klonos’s picture

Issue tags: +Needs backport to D7

.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sukr_s made their first commit to this issue’s fork.

sukr_s’s picture

Issue summary: View changes
Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs steps to reproduce

Can we add steps to the issue summary (part of standard issue template) since this is a bug report.

sukr_s’s picture

Issue summary: View changes
Status: Needs work » Needs review

IS updated.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

sukr_s’s picture

Status: Needs work » Needs review
Issue tags: -Needs steps to reproduce
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative
1) Drupal\FunctionalJavascriptTests\Core\Form\JavascriptStatesTest::testJavascriptStates
Behat\Mink\Exception\ResponseTextException: The text "Enter password" appears in the text of this page, but it should not.
/builds/issue/drupal-1427838/vendor/behat/mink/src/WebAssert.php:907
/builds/issue/drupal-1427838/vendor/behat/mink/src/WebAssert.php:312
/builds/issue/drupal-1427838/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php:234
/builds/issue/drupal-1427838/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php:67
FAILURES!
Tests: 1, Assertions: 92, Failures: 1.

Ran the test-only feature which generated the above, showing the test coverage

Summary appears to be complete.

Code review solution seems simple and accomplishes the issue.

LGTM

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs title update

@sukr_s, thanks for working on an older issue!

I read the issue summary, comments and the MR. There are no unanswered questions.

I left a comment in the MR asking for a comment to be changed. The title here needs to be updated because it refers to 'password' but the MR makes no changes to password, only to password_confirm. Both of those should be simple to complete.

sukr_s’s picture

Title: password and password_confirm children do not pick up #states or #attributes » password_confirm children do not pick up #states or #attributes
Status: Needs work » Needs review

- Changed the title.
- Couldn't locate the MR comment. Nevertheless added a comment at the place of change.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs title update

Going to RTBC because title has been updated.

@quietone I'm not seeing the comment either did it not save?

quietone’s picture

No, I did not submit my earlier comment. I have done so now.

@sukr_s, thanks for figuring out what I meant.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs backport to D7

I discussed this with @tim.plunkett and we agreed that adding another check on render element ID is probably not the best way forward. Instead we could do what the comment says and check if the element accepts input but has no markup. So something like

$key = (isset($elements['#markup']) && $elements['#markup'] === '' && isset($elements['#input']) && $elements['#input'] === TRUE) ? '#wrapper_attributes' : '#attributes';

This way anything in contrib will be fixed as well. We will need update the docs here to...

tim.plunkett’s picture

Issue tags: +Barcelona2024
sukr_s’s picture

Status: Needs work » Needs review

updated as per #32

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Verified test coverage is still there https://git.drupalcode.org/issue/drupal-1427838/-/jobs/2949098

Appears feedback from @alexpott and @tim.plunkett has been addressed I believe.

alexpott’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 36b2bfae686 to 11.x and a63415bb33f to 11.0.x and 4ba08a0254e to 10.4.x and 4951a71170f to 10.3.x. Thanks!

  • alexpott committed 4951a711 on 10.3.x
    Issue #1427838 by sukr_s, smustgrave, quietone, tim.plunkett, alexpott:...

  • alexpott committed 4ba08a02 on 10.4.x
    Issue #1427838 by sukr_s, smustgrave, quietone, tim.plunkett, alexpott:...

  • alexpott committed a63415bb on 11.0.x
    Issue #1427838 by sukr_s, smustgrave, quietone, tim.plunkett, alexpott:...

  • alexpott committed 36b2bfae on 11.x
    Issue #1427838 by sukr_s, smustgrave, quietone, tim.plunkett, alexpott:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

cesarmiquel’s picture

Hi! I have the following code that used to work fine but now it breaks after this has been deployed. Basically I have a form with a list of items that look something like this:

<?php
$address_list = [];
foreach($address as $id) {
  $address_list[$id] = [
    '#type' => 'item',
    '#markup' => '...',
    '#states' => [
      'visible' => [
        ':input[id="some_select"]' => ['value' => $id],
      ]
    ]
  ];
}
$form['addresses'] = $address_list;
?>

This used to work fine before this patch. When you selected an option in the select it would display one of these items which have information related to the selection. I've identified the issue and have verified that this fix is breaking the previous code but I have no clue why when the data-drupal-states is set in the #wrapper_attributes it works but not in the #attributes. Any help? Is there something wrong with the way I'm using the #states? Heelp🙏

steven jones’s picture

@cesarmiquel yeah, I have that exact issue. The logic that was changed in this issue is incorrect. I've raised #3490435: Regression: Form states are processed and can be added to the wrong attribute for 'item' elements to discuss and fix that.

cesarmiquel’s picture

Thanks @steven-jones for your feedback. I will follow this new issue then. Do you know of a workaround for this to work in the meantime?

steven jones’s picture

cesarmiquel’s picture

@steven-jones unfortunately not easily because the site is a multi-site Drupal and I don't have the ability to patch the Drupal Core. I might be able to request a patch but it will take a while (probably a week or maye more **if** they approve it) for them to patch it so I need to find a work around.