Skip to content

Commit 1f059a5

Browse files
committed
Implemented message filter support; closes #25.
1 parent 782502a commit 1f059a5

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/app.provider.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
77
// Display the validation error message below the `input` field with class "help-block"
88
var displayErrorsAs = 'simple';
99

10+
// Can be a string or list of any combination of filters; will be applied in order
11+
// For example: 'lowercase' or ['lowercase', 'reverse'] (So long as the filter(s) exists)
12+
var messageFilters = [];
13+
1014
function shouldValidateOn(event) {
1115
if (angular.isString(validateFieldsOn)) {
1216
return validateFieldsOn === event;
@@ -61,6 +65,22 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
6165
displayErrorsAs = type;
6266
};
6367

68+
this.global.useMessageFilters = function(filters) {
69+
if (!filters) {
70+
throw 'Please provide a string or list of filters to apply to messages';
71+
}
72+
73+
if (!angular.isString(filters) && !angular.isArray(filters)) {
74+
throw 'Filters should either be a string or a list';
75+
}
76+
77+
messageFilters = filters;
78+
79+
if (!angular.isArray(messageFilters)) {
80+
messageFilters = [messageFilters];
81+
}
82+
};
83+
6484
this.$get = [function() {
6585
return {
6686
messages: _this.global.messages,
@@ -76,6 +96,10 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
7696
return _this.global.errorMessagePrefix || '';
7797
},
7898

99+
getMessageFilters: function () {
100+
return messageFilters;
101+
},
102+
79103
getTooltipPlacement: function() {
80104
return _this.global.tooltipPlacement;
81105
},

src/services/validation.service.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @description Core service of this module to provide various default validations.
77
*/
88
angular.module('bootstrap.angular.validation').factory('BsValidationService', ['$interpolate', 'bsValidationConfig',
9-
'$injector', function($interpolate, validationConfig, $injector) {
9+
'$injector', '$filter', function($interpolate, validationConfig, $injector, $filter) {
1010

1111
var displayErrorAsAttrName = 'bsDisplayErrorAs';
1212
var customFormGroup = '[bs-form-group]';
@@ -170,6 +170,7 @@ angular.module('bootstrap.angular.validation').factory('BsValidationService', ['
170170

171171
resolveMessage: function($element, $attr, key) {
172172
var metaInformation = this.getMetaInformation($element);
173+
var messageFilters = $element.attr(key + '-notification-filter') || validationConfig.getMessageFilters();
173174
var message = $element.attr(key + '-notification') || validationConfig.messages[key];
174175

175176
if (!message) {
@@ -179,6 +180,22 @@ angular.module('bootstrap.angular.validation').factory('BsValidationService', ['
179180
message = 'Please fix this field';
180181
}
181182

183+
if (angular.isDefined(messageFilters)) {
184+
if (!angular.isArray(messageFilters)) {
185+
messageFilters = [messageFilters];
186+
}
187+
188+
for (var i = 0; i < messageFilters.length; i++) {
189+
var filter = $filter(messageFilters[i]);
190+
191+
if (!angular.isDefined(filter) || filter === null) {
192+
throw 'The specified message filter \'' + messageFilters[i] + '\' does not exist.';
193+
}
194+
195+
message = filter(message);
196+
}
197+
}
198+
182199
var matchers = angular.extend({}, {validValue: $attr[key]}, metaInformation);
183200
return $interpolate(message)(matchers);
184201
},

0 commit comments

Comments
 (0)