Allow attributes to be passed to the password confirm fields

Created on 12 September 2019, over 5 years ago
Updated 26 July 2024, 5 months ago

Use Case: The password_confirm form field type isn't inheriting the attributes

When a custom form is to be developed that has password and confirm password fields, that need to follow a pattern.

Based on the above requirements password_confirm is the correct form field type to use.

However since password_confirm field type does not inherit #attributes, the pass1 and pass2 fields will not contain the attributes set on the password_confirm field.

In the below example, the test class will not be present on either the pass1 or pass2 fields.

$form['pass'] = array(
  '#type' => 'password_confirm',
  '#title' => $this->t('Password'),
  '#size' => 25,
  '#attributes' => ['class' => 'test']
);

Proposed resolution

Add the ability for attributes to be passed to the password_confirm fields.

Remaining tasks

  • Create change record

API changes

Forms API

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
FormΒ  β†’

Last updated 4 days ago

Created by

πŸ‡§πŸ‡ͺBelgium tim-diels Belgium πŸ‡§πŸ‡ͺ

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    I quite like the suggested approach at #33 of adding a dedicated key for the password_confirm attributes.

    I've added a new patch that supports this, they now no longer inhertit the elements base #attributes, there is a new array key that can be passed called #pass2_attributes. Happy for feedback on the name. I called it this because the password confirmation form element is called #pass2, so this seemed the most consistent.

  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU
  • Status changed to Needs work almost 2 years ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Reviewing #35 and also like the idea of using a 2nd variable.

    Can the new solution approach be added to the issue summary.

    For the remaining tasks will those have to happen after this is merged? Should they be follow ups?

  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU
    +++ b/core/lib/Drupal/Core/Render/Element/PasswordConfirm.php
    @@ -68,28 +68,38 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
    +      '#attributes' => is_array($element['#attributes']) ?
    +      array_merge_recursive($element['#attributes'], $pass1_attributes) :
    +      $pass1_attributes,
    

    #pass1 inherits the attributes from the element. I'll update the IS and write a CR

  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU
  • Status changed to Needs review over 1 year ago
  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    Updated the existing CR - Ready for review

  • Status changed to Needs work over 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia darvanen Sydney, Australia

    Fabulous, thanks @DanielVeza, that just leaves drafting/adding some documentation.

    For anyone wishing to help with that, I'd say the @code example at the top of \Drupal\Core\Render\Element\PasswordConfirm is a good place to put a simple example, perhaps even a copy of the one in the change record.

  • πŸ‡ͺπŸ‡ΈSpain eduardo morales alberti Spain, πŸ‡ͺπŸ‡Ί
  • πŸ‡ͺπŸ‡ΈSpain eduardo morales alberti Spain, πŸ‡ͺπŸ‡Ί

    Not the same issue, but could be related because are altering the same field.

  • Pipeline finished with Failed
    9 months ago
    Total: 512s
    #125753
  • Pipeline finished with Failed
    9 months ago
    Total: 484s
    #125762
  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Lets try to implement this a little more simpler by working with what we have, and not implementing brand new element properties.

    This is a similar methodology to what you can do with checkboxes and/or radios to access the sub elements:

    # Where `$form['account']['pass']` is the `password_confirm` element:
    $form['account']['pass']['pass1']['#attributes']['class'] = ['foobar'];
    

    We just need to update \Drupal\Core\Render\Element\PasswordConfirm::processPasswordConfirm to merge attributes from the passed render array, rather than override:

    app/core/lib/Drupal/Core/Render/Element/PasswordConfirm.php:76

    Old/current:

    '#attributes' => [

    Would change to:

    '#attributes' => $element["pass1"]["#attributes"] ?? [] + [

  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    Agree with the changes that @dpi suggested. Updating the title and the MR to reflect what the new solution will be.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Closed some issues that I think are duplicates, no credit to transfer. Also hiding files.

Production build 0.71.5 2024