Skip to content

Commit

Permalink
Merge pull request #26 from ShaneYu/master
Browse files Browse the repository at this point in the history
Merge implementation for message filters.
  • Loading branch information
sagrawal31 committed Jan 13, 2017
2 parents 782502a + 5728a79 commit bb64a40
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/app.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
// Display the validation error message below the `input` field with class "help-block"
var displayErrorsAs = 'simple';

// Can be a string or list of any combination of filters; will be applied in order
// For example: 'lowercase' or ['lowercase', 'reverse'] (So long as the filter(s) exists)
var messageFilters = [];

function shouldValidateOn(event) {
if (angular.isString(validateFieldsOn)) {
return validateFieldsOn === event;
Expand Down Expand Up @@ -61,6 +65,22 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
displayErrorsAs = type;
};

this.global.useMessageFilters = function(filters) {
if (!filters) {
throw 'Please provide a string or list of filters to apply to messages';
}

if (!angular.isString(filters) && !angular.isArray(filters)) {
throw 'Filters should either be a string or a list';
}

messageFilters = filters;

if (!angular.isArray(messageFilters)) {
messageFilters = [messageFilters];
}
};

this.$get = [function() {
return {
messages: _this.global.messages,
Expand All @@ -76,6 +96,10 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
return _this.global.errorMessagePrefix || '';
},

getMessageFilters: function () {
return messageFilters;
},

getTooltipPlacement: function() {
return _this.global.tooltipPlacement;
},
Expand Down
13 changes: 12 additions & 1 deletion src/services/validation.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @description Core service of this module to provide various default validations.
*/
angular.module('bootstrap.angular.validation').factory('BsValidationService', ['$interpolate', 'bsValidationConfig',
'$injector', function($interpolate, validationConfig, $injector) {
'$injector', '$filter', function($interpolate, validationConfig, $injector, $filter) {

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

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

if (!message) {
Expand All @@ -179,6 +180,16 @@ angular.module('bootstrap.angular.validation').factory('BsValidationService', ['
message = 'Please fix this field';
}

if (angular.isDefined(messageFilters)) {
if (!angular.isArray(messageFilters)) {
messageFilters = [messageFilters];
}

for (var i = 0; i < messageFilters.length; i++) {
message = $filter(messageFilters[i])(message);
}
}

var matchers = angular.extend({}, {validValue: $attr[key]}, metaInformation);
return $interpolate(message)(matchers);
},
Expand Down

0 comments on commit bb64a40

Please sign in to comment.