Last active
August 29, 2015 14:02
-
-
Save scizers/5f5ecf2004192a51762f to your computer and use it in GitHub Desktop.
This is a angularjs directive for ensuring unique feilds in form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.directive('ensureUnique', ['$http', function ($http) { | |
return { | |
require: 'ngModel', | |
link: function (scope, ele, attrs, c) { | |
scope.$watch(attrs.ngModel, function () { | |
if(scope[attrs.ngModel] !== undefined) { | |
$http({ | |
method: 'POST', | |
url: '/' + attrs.ensureUnique, | |
data: {'field': scope[attrs.ngModel] } | |
}).success(function (data, status, headers, cfg) { | |
c.$setValidity('unique', data[0].isUnique); | |
}).error(function (data, status, headers, cfg) { | |
c.$setValidity('unique', false); | |
}); | |
} | |
}); | |
} | |
} | |
}]); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/checkMobile', auth.confirmUser, function (req, res) { | |
if (req.body.field !== undefined) { | |
var mobile = req.body.field; | |
var query = 'SELECT * FROM `users` WHERE `email` LIKE "' + email + '"'; | |
utils.exec(query, {}, function (err, result) { | |
if (!result.length) { | |
res.json([ | |
{isUnique: true} | |
]); | |
} else { | |
res.json([ | |
{isUnique: false} | |
]); | |
} | |
}) | |
} else { | |
res.json([ | |
{isUnique: false} | |
]); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment