Guest User

Untitled

a guest
May 23rd, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. angular.module('auth')
  2. .factory('ValidationService', ['$http', function($http){
  3. var vs = {};
  4. var baseUrl = '/api/validate/';
  5. vs.validate = function(options) {
  6. var param = options.param,
  7. value = options.value,
  8. url = options.url || baseUrl;
  9. if (param && value) {
  10. return $http.get(url, {params: {p: param, v: value}}).then(function(response) {
  11. return response.data.isValid;
  12. });
  13. }
  14. };
  15. return vs;
  16. }]);
  17.  
  18. angular.module('auth')
  19. .directive('unique', function(ValidationService) {
  20. return {
  21. require: 'ngModel',
  22. scope: {
  23. param: '@unique'
  24. },
  25. link: function(scope, elm, attrs, ctrl) {
  26. ctrl.$asyncValidators.unique = function(modelValue, viewValue) {
  27. var val = modelValue || viewValue;
  28. return ValidationService.validate({param: scope.param, value: val});
  29. };
  30. }
  31. }
  32. });
  33.  
  34. <form novalidate class="css-form" name="usrform">
  35. Email: <input type="email" ng-model="user.email" name="email" unique="email" required/><br/>
  36. </form>
  37. <pre>email = "{{usrform.email.$error}}"</pre>
Advertisement
Add Comment
Please, Sign In to add comment