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
Comments
Comment #1
Sji Infotech commentedhi nice Question
i want also this type questions .
Thankful
Comment #2
norman.lolFor me a container around the password_confirm did the trick. Fieldset works too.
Comment #3
chi commentedLets move it forward.
Comment #4
chi commentedComment #5
mglamanConfirming this is still an issue.
Had to implement the following while working on the Drupal Commerce order add form for new customer creation.Update. HTML5 validation prevents this from working.
Comment #14
klonos.
Comment #22
sukr_s commentedComment #23
smustgrave commentedCan we add steps to the issue summary (part of standard issue template) since this is a bug report.
Comment #24
sukr_s commentedIS updated.
Comment #25
needs-review-queue-bot commentedThe 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.
Comment #26
sukr_s commentedComment #27
smustgrave commentedRan 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
Comment #28
quietone commented@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.
Comment #29
sukr_s commented- Changed the title.
- Couldn't locate the MR comment. Nevertheless added a comment at the place of change.
Comment #30
smustgrave commentedGoing to RTBC because title has been updated.
@quietone I'm not seeing the comment either did it not save?
Comment #31
quietone commentedNo, I did not submit my earlier comment. I have done so now.
@sukr_s, thanks for figuring out what I meant.
Comment #32
alexpottI 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
This way anything in contrib will be fixed as well. We will need update the docs here to...
Comment #33
tim.plunkettComment #34
sukr_s commentedupdated as per #32
Comment #35
smustgrave commentedVerified 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.
Comment #36
alexpottCommitted 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!
Comment #42
cesarmiquel commentedHi! 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:
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-statesis set in the#wrapper_attributesit works but not in the#attributes. Any help? Is there something wrong with the way I'm using the #states? Heelp🙏Comment #43
steven jones commented@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.
Comment #44
cesarmiquel commentedThanks @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?
Comment #45
steven jones commented@cesarmiquel you can apply the changes from the MR in #3490435: Regression: Form states are processed and can be added to the wrong attribute for 'item' elements :)
Comment #46
cesarmiquel commented@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.