angularjs - show validation for my login form if no already exist in backend -
i'm using directive populating front-end validation if number present in back-end shows validation every number, whether it's present in back-end or not.
below code sample:
html
<div class="col-xs-8"> <md-input-container> <md-icon md-svg-icon="assets/images/device.svg"></md-icon> <input type="number" placeholder="enter user mobile no" name=mobile" id="mobile" ng-model="mobile" required minlength="10" ng-pattern="/^[789]\d{9}$/" phone-number-validator> <div ng-messages="myform.mobile.$error"> <div ng-message="required">registered mobile no.</div> <div ng-message="phonenumber">the mobile no entered has been registered</div> <div ng-message="test">too short</div> </div> </md-input-container> </div>
directive
angular.module('shoppingpad').directive('phonenumbervalidator',phonenumbervalidator) function phonenumbervalidator($q,loginservices,restservice){ var deferred=$q.defer(); return{ restrict:'ea', require:'ngmodel', link:function($scope,element,attrs,ngmodel){ ngmodel.$asyncvalidators.phonenumber=function(modelvalue , viewvalue) { var mobile= (modelvalue || viewvalue).tostring(); console.log(typeof mobile); if(mobile.length == 10){ console.log('mobile 2' + typeof loginservices.getmobileno()); return restservice.getrequest('save/all?m='+mobile).then(function(response){ console.log("inside rest"); deferred.reject(response.status); return deferred.promise; },function(error){ return deferred.promise; }); } } } } }
service
angular.module('shoppingpad').service('restservice',restservice); function restservice($http){ var port=4005; var baseurl="http://localhost:"+port; this.getrequest=function(path,query){ console.log("we in getrequest"); var def=$http.get(baseurl+"/"+path,query); console.log(def) return def; }; } nodejs router.get('/all',function(req,res){ console.log("abcd"); var mobile=(req.query.m) console.log(typeof mobile); console.log(mobile) user.all(mobile,function(error,data){ if(error){ res.send(error) }else{ res.send(data) } })
});
userlist.prototype.all=function(mobile,cb){
user.find({mobile:mobile},function(err,data){ if(data){ console.log(data); return cb(null,data) } else{ return cb(err,null); } });
};
ok. figured out.
you need create promise inside of asyncvalidator otherwise doing 1 validation ngmodels.
the second mistake returning request's promise resolved/rejected $http service.
angular.module('shoppingpad').directive('phonenumbervalidator', phonenumbervalidator) function phonenumbervalidator($q, loginservices, restservice) { return { restrict: 'ea', require: 'ngmodel', link: function ($scope, element, attrs, ngmodel) { ngmodel.$asyncvalidators.phonenumber = function (modelvalue, viewvalue) { var deferred = $q.defer(); var mobile = (modelvalue || viewvalue).tostring(); if (mobile.length === 10) { restservice.getrequest('save/all?m=' + mobile).then(function (response) { if (response.status === 'mobile exist') { deferred.reject(response.status) } else { deferred.resolve(); } }, function (error) { //todo reject or resolve return deferred.promise; }); } else { //no validation error if length not 10 deferred.resolve(); } return deferred.promise; } } } }
Comments
Post a Comment