Skip to content

Commit 9fac705

Browse files
author
Yeray Diaz Diaz
committed
Disable form and show warning on mismatch when updating password
1 parent 1df15fa commit 9fac705

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,41 @@
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 {
17-
static targets = ["showPassword", "password"];
18+
static targets = ["showPassword", "password", "newPassword", "confirmPassword", "passwordsMatch", "submit"];
1819

1920
connect() {
2021
// Reset these so they don't persist between page reloads
2122
// We assume that both targets above exist
2223
this.showPasswordTarget.checked = false;
2324
this.togglePasswords();
25+
this.checkPasswordsMatch();
2426
}
2527

2628
togglePasswords() {
2729
for (let field of this.passwordTargets) {
2830
field.type = this.showPasswordTarget.checked ? "text" : "password";
2931
}
3032
}
33+
34+
checkPasswordsMatch() {
35+
if (this.passwordTargets.every(field => field.value === "")) {
36+
this.passwordsMatchTarget.classList.add("hidden");
37+
this.submitTarget.setAttribute("disabled", "");
38+
} else {
39+
this.passwordsMatchTarget.classList.remove("hidden");
40+
if (this.newPasswordTarget.value === this.confirmPasswordTarget.value) {
41+
this.passwordsMatchTarget.innerHTML = "Passwords match";
42+
this.passwordsMatchTarget.classList.add("form-error--valid");
43+
this.submitTarget.removeAttribute("disabled");
44+
} else {
45+
this.passwordsMatchTarget.innerHTML = "Passwords do not match";
46+
this.passwordsMatchTarget.classList.remove("form-error--valid");
47+
this.submitTarget.setAttribute("disabled", "");
48+
}
49+
}
50+
}
3151
}

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: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,25 +215,42 @@ <h2 class="sub-title">Change Password</h2>
215215
id="show-password" type="checkbox" tabindex="4">&nbsp;Show passwords
216216
</label>
217217
</div>
218-
{{ change_password_form.password(placeholder="Your current password", required="required", class_="form-group__input", data_target="password.password") }}
218+
{{ change_password_form.password(
219+
placeholder="Your current password",
220+
required="required",
221+
class_="form-group__input",
222+
data_target="password.password password.oldPassword")}}
219223
{{ field_errors(change_password_form.password) }}
220224
</div>
221225
<div class="form-group">
222226
<label class="form-group__label" for="name">New Password</label>
223227
{# the password field needs to be wrapped in a div to properly place tooltips #}
224228
<div>
225-
{{ change_password_form.new_password(placeholder="Select a new password", required="required", class_="form-group__input", data_target="password.password") }}
229+
{{ change_password_form.new_password(
230+
placeholder="Select a new password",
231+
required="required",
232+
class_="form-group__input",
233+
data_target="password.password password.newPassword",
234+
data_action="keyup->password#checkPasswordsMatch") }}
226235
</div>
227236
{{ field_errors(change_password_form.new_password) }}
228237
{{ password_strength_gauge() }}
229238
</div>
230239
<div class="form-group">
231240
<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") }}
241+
{{ change_password_form.password_confirm(
242+
placeholder="Confirm password",
243+
required="required",
244+
class_="form-group__input",
245+
data_target="password.password password.confirmPassword",
246+
data_action="keyup->password#checkPasswordsMatch") }}
233247
{{ field_errors(change_password_form.password_confirm) }}
234248
</div>
249+
<ul class="form-errors">
250+
<li data-target="password.passwordsMatch" class="hidden"></li>
251+
</ul>
235252
<div>
236-
<input value="Update Password" class="button button--primary" type="submit">
253+
<input value="Update Password" class="button button--primary" type="submit" data-target="password.submit">
237254
</div>
238255
</form>
239256

0 commit comments

Comments
 (0)