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