Skip to content

Commit c18eb36

Browse files
author
Yeray Diaz Diaz
committed
Disable form and show warning on mismatch when updating password (#3197)
1 parent 582bc4f commit c18eb36

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

warehouse/static/js/warehouse/controllers/password_controller.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
*/
14+
1415
import { Controller } from "stimulus";
1516

1617
export default class extends Controller {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
import { Controller } from "stimulus";
16+
17+
export default class extends Controller {
18+
static targets = ["passwordMatch", "matchMessage", "submit"];
19+
20+
connect() {
21+
this.checkPasswordsMatch();
22+
}
23+
24+
checkPasswordsMatch() {
25+
if (this.passwordMatchTargets.every(field => field.value === "")) {
26+
this.matchMessageTarget.classList.add("hidden");
27+
this.submitTarget.setAttribute("disabled", "");
28+
} else {
29+
this.matchMessageTarget.classList.remove("hidden");
30+
if (this.passwordMatchTargets.every((field, i, arr) => field.value === arr[0].value)) {
31+
this.matchMessageTarget.innerHTML = "Passwords match";
32+
this.matchMessageTarget.classList.add("form-error--valid");
33+
this.submitTarget.removeAttribute("disabled");
34+
} else {
35+
this.matchMessageTarget.innerHTML = "Passwords do not match";
36+
this.matchMessageTarget.classList.remove("form-error--valid");
37+
this.submitTarget.setAttribute("disabled", "");
38+
}
39+
}
40+
}
41+
}

warehouse/static/sass/blocks/_form-errors.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,17 @@
4646
max-width: 100%;
4747
}
4848
}
49+
50+
.form-error {
51+
&--valid {
52+
color: $success-color;
53+
54+
&:before {
55+
font-family: "FontAwesome";
56+
content: "\f00c"; // Check / Tick
57+
margin-right: 5px;
58+
color: $success-color;
59+
}
60+
}
61+
}
4962
}

warehouse/templates/manage/account.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,37 +203,39 @@ <h2 class="sub-title">Account Emails</h2>
203203

204204
<h2 class="sub-title">Change Password</h2>
205205
{{ form_error_anchor(change_password_form) }}
206-
<form data-controller="password" method="POST" action="#errors">
206+
<form data-controller="password password-match" method="POST" action="#errors">
207207
<input name="csrf_token" type="hidden" value="{{ request.session.get_csrf_token() }}">
208208
{{ form_errors(change_password_form) }}
209209
<div class="form-group">
210-
211210
<div class="split-layout">
212211
<label class="form-group__label" for="name">Old Password</label>
213212
<label for="show-password">
214213
<input data-action="change->password#togglePasswords" data-target="password.showPassword"
215214
id="show-password" type="checkbox" tabindex="4">&nbsp;Show passwords
216215
</label>
217216
</div>
218-
{{ change_password_form.password(placeholder="Your current password", required="required", class_="form-group__input", data_target="password.password") }}
217+
{{ change_password_form.password(placeholder="Your current password", required="required", class_="form-group__input", data_target="password.password")}}
219218
{{ field_errors(change_password_form.password) }}
220219
</div>
221220
<div class="form-group">
222221
<label class="form-group__label" for="name">New Password</label>
223222
{# the password field needs to be wrapped in a div to properly place tooltips #}
224223
<div>
225-
{{ change_password_form.new_password(placeholder="Select a new password", required="required", class_="form-group__input", data_target="password.password") }}
224+
{{ change_password_form.new_password(placeholder="Select a new password", required="required", class_="form-group__input", data_target="password.password password-match.passwordMatch", data_action="keyup->password-match#checkPasswordsMatch") }}
226225
</div>
227226
{{ field_errors(change_password_form.new_password) }}
228227
{{ password_strength_gauge() }}
229228
</div>
230229
<div class="form-group">
231230
<label class="form-group__label" for="name">Confirm New Password</label>
232-
{{ change_password_form.password_confirm(placeholder="Confirm password", required="required", class_="form-group__input", data_target="password.password") }}
231+
{{ change_password_form.password_confirm(placeholder="Confirm password", required="required", class_="form-group__input", data_target="password.password password-match.passwordMatch", data_action="keyup->password-match#checkPasswordsMatch") }}
233232
{{ field_errors(change_password_form.password_confirm) }}
234233
</div>
234+
<ul class="form-errors">
235+
<li data-target="password-match.matchMessage" class="hidden"></li>
236+
</ul>
235237
<div>
236-
<input value="Update Password" class="button button--primary" type="submit">
238+
<input value="Update Password" class="button button--primary" type="submit" data-target="password-match.submit">
237239
</div>
238240
</form>
239241

0 commit comments

Comments
 (0)