Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module('auth')
- .factory('ValidationService', ['$http', function($http){
- var vs = {};
- var baseUrl = '/api/validate/';
- vs.validate = function(options) {
- var param = options.param,
- value = options.value,
- url = options.url || baseUrl;
- if (param && value) {
- return $http.get(url, {params: {p: param, v: value}}).then(function(response) {
- return response.data.isValid;
- });
- }
- };
- return vs;
- }]);
- angular.module('auth')
- .directive('unique', function(ValidationService) {
- return {
- require: 'ngModel',
- scope: {
- param: '@unique'
- },
- link: function(scope, elm, attrs, ctrl) {
- ctrl.$asyncValidators.unique = function(modelValue, viewValue) {
- var val = modelValue || viewValue;
- return ValidationService.validate({param: scope.param, value: val});
- };
- }
- }
- });
- <form novalidate class="css-form" name="usrform">
- Email: <input type="email" ng-model="user.email" name="email" unique="email" required/><br/>
- </form>
- <pre>email = "{{usrform.email.$error}}"</pre>
Advertisement
Add Comment
Please, Sign In to add comment