jonsamwell / angular-auto-validate

An automatic validation module for AngularJS which gets rid of excess html in favour of dynamic element modification to notify the user of validation errors
MIT License
346 stars 76 forks source link

Value {0} replace dont work when use myCustomErrorMessageResolver #164

Closed IgoPeg closed 7 years ago

IgoPeg commented 7 years ago

When i use the myCustomErrorMessageResolver that returns a promise example:

.factory('myCustomErrorMessageResolver', [ '$translate', function ($translate) { 'use strict'; return { resolve: function (errorType) { return $translate('FORMS.' + errorType); // will return "Bitte eine Zahl von mindestens {0} eingeben." } }; } ])

The {0} will not replaced by specific value. Example on ng-minlength="6"

IgoPeg commented 7 years ago

Solution:

     resolve: function (errorType, el) {

      var parameter = el.attr('ng-' + errorType);
      if (parameter === undefined) {
        parameter = el.attr('data-ng-' + errorType) || el.attr(errorType);
      }

      parameters.push(parameter || '');

      return $translate('FORMS.' + errorType).then(function (ms) {

        return ms.replace(/{(\d+)}/g, function (match, number) {
          return typeof parameters[number] !== undefined ? parameters[number] : match;
        });

      });

    }