如何进行常见的独特验证?

时间:2016-03-21 13:47:46

标签: javascript angularjs node.js mongoose mean-stack

我是MEAN STACK网络应用程序的新手 我想进行通用的独特验证,验证来自现场的模糊。

Register.html

 static void channel_BasicNacks(object sender, RabbitMQ.Client.Events.BasicNackEventArgs e)
    {

    }

    static void channel_BasicAcks(object sender, RabbitMQ.Client.Events.BasicAckEventArgs e)
    {

    }

directive.js

<input type="email" ng-model="user.email" name="email" placeholder="Enter your Email" ng-unique="User.email" required />
<span ng-show="myForm.email.$error.unique">Email already exists</span>

route.js

myapp.directive('ngUnique', ['$http', function($http) {
  return {
        require: 'ngModel',
        link: function(scope, elem, attrs, ctrl) {
              elem.on('blur', function(evt) {
                    scope.$apply(function() {
                          $http({
                                method: 'POST',
                                url: '/api/checkUnique',
                                data: {
                                      dbFieldValue: elem.val(),
                                      dbField: attrs.ngUnique
                                }
                          }).success(function(data, status, headers, config) {
                                ctrl.$setValidity('unique', data.status);
                          });
                    });
              });
        }
  }
}]);

依赖关系

 var User = require('./models/user');

  app.post('/api/checkUnique', function(req, res) {
        //console.log(req.body);
        modelNameAndFieldName = req.body.dbField.split(".");
        modelName = modelNameAndFieldName[0];
        modelFieldName = modelNameAndFieldName[1];
        modelFieldValue = req.body.dbFieldValue;

        /*modelName.count({ modelFieldName: modelFieldValue }, function(err, data) {
              if (err) {
                    res.json({ "status": false });
              } else {
                    res.json({ "status": data > 0 ? true : false });
              }
        });*/

        User.count({ email: modelFieldValue }, function(err, data) {
              if (err) {
                    res.json({ "status": false });
              } else {
                    res.json({ "status": data > 0 ? false : true });
              }
        });

  });

上面的代码运行完美,但我希望这个功能很常见,这样在我的应用程序中我可以在任何模式和任何字段中使用它。

0 个答案:

没有答案