Skip to content

Commit

Permalink
Fixed directive to work with both password fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
pbuda committed Jan 29, 2013
1 parent 4b33640 commit 515d289
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
3 changes: 3 additions & 0 deletions bootstrap-ui/src/main/webapp/app/controllers.js
Expand Up @@ -305,6 +305,9 @@ controllers.controller("ProfileController", function ProfileController($scope, U
$scope.newPasswordRepeated = undefined;

$scope.changePassword = function () {
$scope.passwordChangeForm.currentPassword.$dirty = true;
$scope.passwordChangeForm.newPassword.$dirty = true;
$scope.passwordChangeForm.newPasswordRepeated.$dirty = true;
if ($scope.passwordChangeForm.$valid) {
ProfileService.changePassword($scope.currentPassword, $scope.newPassword, function () {
showInfoMessage("Password changed!");
Expand Down
21 changes: 13 additions & 8 deletions bootstrap-ui/src/main/webapp/app/directives.js
Expand Up @@ -10,18 +10,23 @@ directives.directive("bsBlur", function() {
}
});

directives.directive("stringMatches", function() {
directives.directive("repeatPassword", function() {
return {
require: "ngModel",
link: function(scope, elem, attrs, ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
if(viewValue === scope[attrs.stringMatches]) {
ctrl.$setValidity("match", true);
return viewValue;
} else {
ctrl.$setValidity("match", false);
return undefined;
var otherInput = elem.inheritedData("$formController")[attrs.repeatPassword];

ctrl.$parsers.push(function(value) {
if(value === otherInput.$viewValue) {
ctrl.$setValidity("repeat", true);
return value;
}
ctrl.$setValidity("repeat", false);
});

otherInput.$parsers.push(function(value) {
ctrl.$setValidity("repeat", value === ctrl.$viewValue);
return value;
});
}
};
Expand Down
6 changes: 3 additions & 3 deletions bootstrap-ui/src/main/webapp/partials/secured/profile.html
Expand Up @@ -32,7 +32,7 @@
</div>

<div class="offset3 span7">
<form name="passwordChangeForm" class="form-horizontal" ng-submit="changePassword()">
<form name="passwordChangeForm" class="form-horizontal" ng-submit="changePassword()" novalidate>

This comment has been minimized.

Copy link
@adamw

adamw Jan 29, 2013

Member

Why novalidate?

This comment has been minimized.

Copy link
@pbuda

pbuda Jan 29, 2013

Author Contributor

This tells the browser to skip validation of the form and instead Angular takes over. Without this, Angular's validation of 'required' doesn't work as expected.

This comment has been minimized.

Copy link
@adamw

adamw Jan 30, 2013

Member

Ok, is this a bug in Angular or a feature? ;)

This comment has been minimized.

Copy link
@pbuda

pbuda Jan 30, 2013

Author Contributor

It's a feature ;) When browser validates form which has a required field it displays some default popup near input but we want everything to be more consistent since we introduce our own validation. Hence the need to turn off default validation adn rely on Angular. Otherwise some validations will look different to others.

This comment has been minimized.

Copy link
@adamw

adamw Jan 30, 2013

Member

Ah, ok :)

<fieldset>
<legend>Password</legend>
</fieldset>
Expand Down Expand Up @@ -62,11 +62,11 @@

<div class="controls">
<input type="password" id="newPasswordRepeated" name="newPasswordRepeated"
ng-model="newPasswordRepeated" string-matches="newPassword" required>
ng-model="newPasswordRepeated" repeat-password="newPassword" required>
<span class="text-error"
ng-show="passwordChangeForm.newPasswordRepeated.$dirty && passwordChangeForm.newPasswordRepeated.$error.required">Repeat password</span>
<span class="text-error"
ng-show="passwordChangeForm.newPasswordRepeated.$dirty && passwordChangeForm.newPasswordRepeated.$error.match">Passwords don't match</span>
ng-show="passwordChangeForm.newPasswordRepeated.$dirty && passwordChangeForm.newPasswordRepeated.$error.repeat">Passwords don't match</span>
</div>
</div>

Expand Down

0 comments on commit 515d289

Please sign in to comment.