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

custom error message (help) #169

Open ohnooo opened 7 years ago

ohnooo commented 7 years ago

First I want to thank you for this awesome module.... I having some trouble setting up custom error messages through defaultErrorMessageResolver.

I have gone through the documentation on creating customErrorMessageResolver but I was wondering if there is a simpler way.....

I am currently using Spring which already has i18 message resolver so I do not need set the culture.

Questions: In the document, Error Message Resolver, is there a way to pass in data from a scope or a from a service??

errorMessages['myCustomError'] = 'My custom error message'; Instead of hardcoding a string would like to pass in a variable that is retrieved from the server.

So for example....

Resource

app.factory("Data" , function($resource){
    return $resource("http://localhost:8080/root/spa/data/:id", {id: "@id"}, {
        update:{
            method:'PUT'
        }
    });
});

Service that gets all the data needed for the page... something like this

angular.service("DataService", function(Data, $q){
   var self = {
       // ... some variables ...

       'messages' : null,
       loadPageData: function(){

           Data.get(function(data){             
        self.itemData = data.results;

        // Load Message Data
        self.messages = data.messages;  // data.messages is a key, value pare
      });
       }
   }
});
angular.module('jcs-autoValidate')
    .run([
    'DataService',
    'defaultErrorMessageResolver',
    function (DataService, defaultErrorMessageResolver) {
        defaultErrorMessageResolver.getErrorMessages().then(function (errorMessages) {
          errorMessages['myCustomError'] = DataService.messages.myCustomError;
          errorMessages['anotherErrorMessage'] = DataService.messages.anotherErrorMessage;
        });
    }
]);

This does not work because angular.module('jcs-autoValidate').run runs before the DataService is ready. I have tried passing in $qPromise but still no luck... I still get an undefined on DataService.

app.run([
    'defaultErrorMessageResolver',
    '$q',
    'DataService',
    function (defaultErrorMessageResolver, $q, $scope, DataService) {
        console.log('run start');
        var d = $q.defer();

        defaultErrorMessageResolver.getErrorMessages().then(function (errorMessages, DataService) {
            console.log('inside defaultErrorMessageResolver');

            console.log(BookService);
            $scope.dataService = DataService;
            console.log('run',$scope.bookService);
            errorMessages['blankBookName'] = $scope.dataService.messages.NotBlank_book_name;

            d.resolve();

        });

        return d.promise;

    }
]);

Any insights is greatly appreciated.