angular-auto-validate icon indicating copy to clipboard operation
angular-auto-validate copied to clipboard

custom error message (help)

Open ohnooo opened this issue 8 years ago • 0 comments
trafficstars

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.

ohnooo avatar Apr 09 '17 09:04 ohnooo