Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't override autocomplete on PasswordField #9816

Open
jellygnite opened this issue Jan 6, 2021 · 7 comments
Open

Can't override autocomplete on PasswordField #9816

jellygnite opened this issue Jan 6, 2021 · 7 comments

Comments

@jellygnite
Copy link

jellygnite commented Jan 6, 2021

Affected Version

v4.7.0

Description

I couldn't set the attribute 'autocomplete'='new-password' on the ConfirmPasswordField. Seems to only allow on or off.

I suggest transposing the two arrays in the array_merge below in PasswordField.php

public function getAttributes()
    {
        $attributes = [];

        if (!$this->getAllowValuePostback()) {
            $attributes['value'] = null;
        }

        $autocomplete = $this->config()->get('autocomplete');

        if ($autocomplete) {
            $attributes['autocomplete'] = 'on';
        } else {
            $attributes['autocomplete'] = 'off';
        }

        return array_merge(
            parent::getAttributes(),
            $attributes
        );
    }

to

        return array_merge(
            $attributes,
           parent::getAttributes()
          
        );

Apologies if this isn't

PRs

@emteknetnz
Copy link
Member

Thanks for your suggestion! Just to let you know we're closing this feature request as GitHub issues are not the best place to discuss potential enhancements.

autocomplete has multiple valid option beyond on and off https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete. There would probably need to me more thought in to a robust solution rather than just flipping the order of array_merge().

You can read more about the guide on contributing issues and you are welcome to raise new feature ideas on the Silverstripe forum instead.

@kinglozzer
Copy link
Member

If PasswordField only sets 2 keys, we could probably simplify the method by removing the array_merge:

public function getAttributes()
{
    $attributes = parent::getAttributes();

    if (!$this->getAllowValuePostback()) {
        $attributes['value'] = null;
    }

    if (!array_key_exists('autocomplete', $attributes)) {
        $attributes['autocomplete'] = $this->config()->get('autocomplete') ? 'on' : 'off';
    }

    return $attributes;
}

Fancy submitting a PR to fix this @jellygnite? It’s arguably a fix, so I can’t see any reason not to submit it against the 4.7 branch

@emteknetnz
Copy link
Member

@kinglozzer I'm not sure about that solution above, because it would change the existing behavior so that a parent class value of 'autocomplete' would override any configuration value on the class for 'autocomplete'. This may break some existing sites.

To me it seems like a better solution would be to test if the config value of autocomplete looks true (true|1|on|yes) = 'on' or looks false (false|0|off}no) to not change behaviour on any existing sites, otherwise if the config value is set to something else such as 'new-password' then use that

@kinglozzer
Copy link
Member

I'm not sure about that solution above, because it would change the existing behavior so that a parent class value of 'autocomplete' would override any configuration value on the class for 'autocomplete'. This may break some existing sites.

I’m not sure what you mean by this? There’s no default value set in the parent class (TextField), so someone would’ve had to manually called ->setAttribute('autocomplete', 'foo') for any value to be set. In that case, they’d be expecting that value to appear so if PasswordField is overwriting it with on/off then that’s a bug IMO.

otherwise if the config value is set to something else such as 'new-password' then use that

I’d rather avoid using the config value for anything other than a fallback when autocomplete hasn’t manually been set. Otherwise it becomes very difficult to set different autocomplete values for different instances of the field. E.g. you might have “old password” and “new password” in the same form - you’d want different autocomplete values for those.

@emteknetnz
Copy link
Member

@kinglozzer ug, sorry my understanding of this was very poor. I confused setting a field on a parent class with simply setting PasswordField::create()->setAttribute('autocomplete', 'myvalue') - sorry about that!

Your approach does look good.

@kinglozzer
Copy link
Member

No worries @emteknetnz 😄. I’ll re-open this and submit a PR soon if @jellygnite doesn’t pick this up first

@kinglozzer kinglozzer reopened this Jan 12, 2021
@emteknetnz
Copy link
Member

This might be worth a read before you got too far with this silverstripe/cwp-core#94

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants