Skip to content

Instantly share code, notes, and snippets.

@auser
Created September 2, 2013 21:34
Show Gist options
  • Select an option

  • Save auser/6417470 to your computer and use it in GitHub Desktop.

Select an option

Save auser/6417470 to your computer and use it in GitHub Desktop.

Revisions

  1. auser created this gist Sep 2, 2013.
    37 changes: 37 additions & 0 deletions Validationexample.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    var app = angular.module('validationExample', []);

    app.controller('signupController', ['$scope', function($scope) {
    $scope.submitted = false;
    $scope.signupForm = function() {
    if ($scope.signup_form.$valid) {

    } else {
    $scope.signup_form.submitted = true;
    }
    }
    }]);

    app.directive('ensureUnique', ['$http', '$timeout', function($http, $timeout) {
    var checking = null;
    return {
    require: 'ngModel',
    link: function(scope, ele, attrs, c) {
    scope.$watch(attrs.ngModel, function(newVal) {
    if (!checking) {
    checking = $timeout(function() {
    $http({
    method: 'POST',
    url: '/api/check/' + attrs.ensureUnique,
    data: {'field': attrs.ensureUnique}
    }).success(function(data, status, headers, cfg) {
    c.$setValidity('unique', data.isUnique);
    checking = null;
    }).error(function(data, status, headers, cfg) {
    checking = null;
    });
    }, 500);
    }
    });
    }
    }
    }]);