<p>I <a href="https://github.com/craftcms/docs/pull/206" rel="nofollow noreferrer">raised a PR</a> for this last week but it was closed. Apparently you're not supposed to load those templates from the front end as those variables are not auto-populated by craft. (<a href="https://github.com/craftcms/docs/pull/206/commits/8ad7fd8ea1a7b3915d77d1b2a9fe3bebadafa46c" rel="nofollow noreferrer">This code</a> fixes the problem)</p>

CC BY-SA 4.0
2018-10-30T15:13:53.450Z
Note: use that on your own risk. I wouldn't recommend doing so
Robin Schambach
CC BY-SA 4.0
2018-10-30T12:45:47.647Z
2018-10-30T12:45:47.647Z
billythekid
<p>The code you are currently using is for the <code>Set Password</code> form that is used when a user forgets their password and you send them a <code>Password reset email</code> as documented at <a href="https://docs.craftcms.com/v3/dev/examples/set-password-form.html" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/examples/set-password-form.html</a>.</p>

<p>Here is what I'm using to allow users to <strong>change</strong> their passwords on demand. It is based on the <code>User Profile Form</code> documented at <a href="https://docs.craftcms.com/v3/dev/examples/user-profile-form.html" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/examples/user-profile-form.html</a></p>

<pre><code>&lt;form method="post" accept-charset="UTF-8"&gt;
    {{ csrfInput() }}
    &lt;input type="hidden" name="action" value="users/save-user"&gt;
    {# Change to suit your needs #}
    {{ redirectInput('users/'~currentUser.username) }}
    {# Get logged-in user's ID #}
    &lt;input type="hidden" name="userId" value="{{ currentUser.id }}"&gt;

    {% macro errorList(errors) %}
        {% if errors %}
            &lt;ul class="errors"&gt;
                {% for error in errors %}
                    &lt;li&gt;{{ error }}&lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        {% endif %}
    {% endmacro %}

    {% from _self import errorList %}

    {# Enter current password #}
    &lt;h3&gt;&lt;label for="password"&gt;Current Password&lt;/label&gt;&lt;/h3&gt;
    &lt;input id="password" type="password" name="password"&gt;

    {% if account is defined %}
        {{ errorList(user.getErrors('password')) }}
    {% endif %}

    {# Enter new password #}
    &lt;h3&gt;&lt;label for="newPassword"&gt;New Password&lt;/label&gt;&lt;/h3&gt;
    &lt;input id="newPassword" type="password" name="newPassword"&gt;

    {% if account is defined %}
        {{ errorList(user.getErrors('newPassword')) }}
    {% endif %}

    &lt;input type="submit" value="Submit"&gt;
&lt;/form&gt;
</code></pre>

<p>UPDATE: updated form to include error handling.</p>

CC BY-SA 4.0
2018-10-30T14:29:06.017Z
2018-10-30T14:46:41.243Z
2018-10-30T14:46:41.243Z
Andrea DeMers
<p>I'm trying to modify Craft 3's example '<a href="https://docs.craftcms.com/v3/dev/examples/set-password-form.html#app" rel="nofollow noreferrer">Change password</a>' form to work with a logged in Commerce 2 user, and have copied that code to <code>templates/setpassword.twig</code>.</p>

<p>Two problems...</p>

<p>(1) The {{ code }} and {{ id }} variables throw <em>Don't exist</em> errors.  </p>

<p>(2) If I comment those variables out, I get a <em>Page not found/"Unable to resolve the request: users/setPassword"</em> error when submitting the form.</p>

<p>Clearly I'm doing this totally wrong ... can anyone set me straight?</p>

<pre><code>&lt;form method="post" accept-charset="UTF-8"&gt;
{{ csrfInput() }}
&lt;input type="hidden" name="action" value="users/set-password"&gt;
&lt;input type="hidden" name="code" value="{{ code }}"&gt;
&lt;input type="hidden" name="id" value="{{ id }}"&gt;

&lt;h3&gt;&lt;label for="newPassword"&gt;New Password&lt;/label&gt;&lt;/h3&gt;
&lt;input id="newPassword" type="password" name="newPassword"&gt;
{% if errors is defined %}
    &lt;ul class="errors"&gt;
        {% for error in errors %}
            &lt;li&gt;{{ error }}&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endif %}

&lt;input type="submit" value="Submit"&gt;
</code></pre>

<p></p>

CC BY-SA 4.0
2018-10-30T12:09:39.303Z
Is this for when a user forgets their password, or do you want a form that allows them to change their password at their leisure?
Andrea DeMers
CC BY-SA 4.0
2018-10-30T13:46:09.873Z
One that lets them change it at leisure : )
James Greig
<password><frontend-form><plugin-craftcommerce2>
Frontend form to change Craft Commerce user's password?
# Frontend form to change Craft Commerce user's password?

(Asked by: username_0 on 2018-10-30)

I'm trying to modify Craft 3's example 'Change password' form to work with a logged in Commerce 2 user, and have copied that code to templates/setpassword.twig.
Two problems...
(1) The {{ code }} and {{ id }} variables throw Don't exist errors.  
(2) If I comment those variables out, I get a Page not found/"Unable to resolve the request: users/setPassword" error when submitting the form.
Clearly I'm doing this totally wrong ... can anyone set me straight?

{{ csrfInput() }}




New Password

{% if errors is defined %}
    
        {% for error in errors %}
            {{ error }}
        {% endfor %}
    
{% endif %}

**Tags:** password, frontend-form, plugin-craftcommerce2

> **Comments:**
> **username_1**: Is this for when a user forgets their password, or do you want a form that allows them to change their password at their leisure?  
> **username_0**: One that lets them change it at leisure : )  

---

## 2 answers

### Answer by username_1 on 2018-10-30. Score: 4 | Accepted answer

The code you are currently using is for the Set Password form that is used when a user forgets their password and you send them a Password reset email as documented at https://docs.craftcms.com/v3/dev/examples/set-password-form.html.
Here is what I'm using to allow users to change their passwords on demand. It is based on the User Profile Form documented at https://docs.craftcms.com/v3/dev/examples/user-profile-form.html

    {{ csrfInput() }}
    
    {# Change to suit your needs #}
    {{ redirectInput('users/'~currentUser.username) }}
    {# Get logged-in user's ID #}
    

    {% macro errorList(errors) %}
        {% if errors %}
            
                {% for error in errors %}
                    {{ error }}
                {% endfor %}
            
        {% endif %}
    {% endmacro %}

    {% from _self import errorList %}

    {# Enter current password #}
    Current Password
    

    {% if account is defined %}
        {{ errorList(user.getErrors('password')) }}
    {% endif %}

    {# Enter new password #}
    New Password
    

    {% if account is defined %}
        {{ errorList(user.getErrors('newPassword')) }}
    {% endif %}

    


UPDATE: updated form to include error handling.


---

### Answer by username_2 on 2018-10-30. Score: -1

I [raised a PR](https://github.com/craftcms/docs/pull/206) for this last week but it was closed. Apparently you're not supposed to load those templates from the front end as those variables are not auto-populated by craft. ([This code](https://github.com/craftcms/docs/pull/206/commits/8ad7fd8ea1a7b3915d77d1b2a9fe3bebadafa46c) fixes the problem)

> **Comments:**
> **username_3**: Note: use that on your own risk. I wouldn't recommend doing so  

---

