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;
}

Comments

Sji Infotech’s picture

hi nice Question
i want also this type questions .

Thankful

leymannx’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.