Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit f39a309

Browse files
committed
Merge pull request #160 from appirio-tech/register-error-messages
Show all email errors at the appropriate time and fix constant validation
2 parents 7f6d89a + a64ad62 commit f39a309

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

app/account/register/register.jade

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
p.form-error(ng-show="vm.registerForm.username.$error.minlength || vm.registerForm.username.$error.maxlength") Username must be between 2 and 15 characters.
5151

5252
.validation-bar(ng-class="{ 'error-bar': (vm.registerForm.email.$dirty && vm.registerForm.email.$invalid), 'success-bar': (vm.registerForm.email.$valid) }")
53-
input(right-placeholder, focused-placeholder="Email", ng-model="vm.email", ng-focus="vm.emailTips = true", ng-blur="vm.emailTips = false", name="email", placeholder="Enter Your Email", type="email", valid-email, email-is-available, required)
53+
input(right-placeholder, focused-placeholder="Email", ng-model="vm.email", ng-model-options="{ debounce: {'default': 500} }", ng-focus="vm.emailTips = true", ng-blur="vm.emailTips = false", name="email", placeholder="Enter Your Email", type="email", valid-email, email-is-available, required)
5454

5555
.tips.email-tips(ng-show="vm.emailTips")
5656
h3 Email Tips:
@@ -60,7 +60,9 @@
6060
p We will occasionally send you emails related to your account or interests.
6161

6262
.form-errors
63-
p.form-error(ng-show="vm.registerForm.email.$dirty && vm.registerForm.email.$invalid") Please enter a valid email address.
63+
p.form-error(ng-show="vm.registerForm.email.$dirty && (vm.registerForm.email.$error.emailIsAvailable || vm.registerForm.email.$error.validEmail)", ng-bind="vm.emailErrorMessage")
64+
65+
p.form-error(ng-show="vm.registerForm.email.$dirty && vm.registerForm.email.$error.required") Please enter an email address.
6466

6567
.validation-bar(ng-class="{ 'success-bar': (vm.registerForm.password.$valid) }")
6668
toggle-password-with-tips(ng-if="!vm.isSocialRegistration")
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
(function() {
22
'use strict';
33

4-
angular.module('tcUIComponents')
5-
.directive('validEmail', validEmail);
6-
4+
angular.module('tcUIComponents').directive('validEmail', validEmail);
5+
76
function validEmail() {
87
return {
98
require: 'ngModel',
109
link: function(scope, element, attrs, ctrl) {
1110
ctrl.$validators.validEmail = function(modelValue, viewValue) {
11+
if (ctrl.$isEmpty(modelValue)) {
12+
return true;
13+
}
14+
1215
if (/.+@.+\..+/.test(viewValue)) {
1316
return true;
1417
}
18+
19+
scope.vm.emailErrorMessage = 'Please enter a valid email address.';
1520
return false;
1621
};
1722
}
1823
};
1924
}
20-
})();
25+
})();

app/directives/account/validate-register.directive.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
};
5151
}
5252

53-
5453
usernameIsFree.$inject = ['UserService', '$log', '$q'];
5554

5655
function usernameIsFree(UserService, $log, $q) {
@@ -60,21 +59,19 @@
6059
ctrl.$asyncValidators.usernameIsFree = function(modelValue, viewValue) {
6160
$log.info('Validating username: ' + modelValue);
6261

63-
// Check if the username exists
6462
var defer = $q.defer();
65-
UserService.validateUserHandle(modelValue).then(
66-
function(data) {
67-
if (data.valid) {
68-
// username is free
69-
return defer.resolve();
70-
} else {
71-
return defer.reject(data.reasonCode);
72-
}
73-
}
74-
).catch(function(resp) {
75-
// call failed - assuming username is free, register call will fail anyways
63+
64+
UserService.validateUserHandle(modelValue).then(function(res) {
65+
if (res.valid) {
7666
return defer.resolve();
67+
} else {
68+
return defer.reject(res.reasonCode);
69+
}
70+
}).catch(function(err) {
71+
// call failed - assuming username is free, register call will fail anyways
72+
return defer.resolve();
7773
});
74+
7875
return defer.promise;
7976
};
8077
}
@@ -90,21 +87,34 @@
9087
ctrl.$asyncValidators.emailIsAvailable = function(modelValue, viewValue) {
9188
$log.info('Validating email: ' + modelValue);
9289

93-
// Check if the username exists
9490
var defer = $q.defer();
95-
UserService.validateUserEmail(modelValue).then(
96-
function(data) {
97-
if (data.valid) {
98-
// email is available
99-
return defer.resolve();
100-
} else {
101-
return defer.reject(data.reasonCode);
91+
92+
UserService.validateUserEmail(modelValue).then(function(res) {
93+
if (res.valid) {
94+
return defer.resolve();
95+
} else {
96+
switch (res.reasonCode) {
97+
case 'ALREADY_TAKEN':
98+
scope.vm.emailErrorMessage = 'That email address is already taken.';
99+
break;
100+
case 'INVALID_EMAIL':
101+
scope.vm.emailErrorMessage = 'Please enter a valid email address.';
102+
break;
103+
case 'INVALID_LENGTH':
104+
scope.vm.emailErrorMessage = 'Email address should be 100 characters or less.';
105+
break;
106+
default:
107+
scope.vm.emailErrorMessage = 'Please enter a valid email address.';
102108
}
109+
110+
return defer.reject(res.reasonCode);
103111
}
104-
).catch(function(resp) {
105-
// call failed - assuming available is free, register call will fail anyways
106-
return defer.resolve();
112+
}).
113+
catch(function(err) {
114+
// call failed - assuming available is free, register call will fail anyways
115+
return defer.resolve();
107116
});
117+
108118
return defer.promise;
109119
};
110120
}

0 commit comments

Comments
 (0)