Skip to content

Commit bb64a40

Browse files
authored
Merge pull request #26 from ShaneYu/master
Merge implementation for message filters.
2 parents 782502a + 5728a79 commit bb64a40

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-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: 12 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,16 @@ 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+
message = $filter(messageFilters[i])(message);
190+
}
191+
}
192+
182193
var matchers = angular.extend({}, {validValue: $attr[key]}, metaInformation);
183194
return $interpolate(message)(matchers);
184195
},

0 commit comments

Comments
 (0)