服务器端验证表单Angular.js [英] Server-side validation form Angular.js

查看:167
本文介绍了服务器端验证表单Angular.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的作品像预期的那样形式的默认验证。但是,当在一个有效的电子邮件地址的用户类型和三个最小字符的密码,这并不意味着登录凭据是有效的。

The default-validation for the form I have works as expected. But when a user types in a valid email address and a password of three characters minimum, that doesn't mean the login credentials are valid.

所以我的问题是我怎么能设置模式的电子邮件的'和'的密码的'无效的服务器端验证之后,这样输入字段获得类NG-无效,而不是NG-有效。

So my question is how can I set the model 'email' and 'password' to invalid after server-side validation, so the input-fields get the class ng-invalid instead of ng-valid.

我目前的code:

function IndexCtrl( $scope, $http )
{
  $scope.form = {};
  $scope.submitLogin = function ()
  {
    $http.post( '/api/auth/login', $scope.form ).success( function( data )
    {
      if ( !data.success )
      {
        $scope.form.errors = [ data ];

        // here I also want to mark the models 'email' and 'password' as invalid, so they both get the class 'ng-invalid'
      }
      else
      {
        $location.path( '/' );
      }
    });
  };
}

任何帮助是值得欢迎的,真正AP preciated。在此先感谢!

Any help is welcome and really appreciated. Thanks in advance!

推荐答案

托什shimayama 给出了正确的答案。 $ setValidity 是从 NgModelController 的方法和有两个参数: validationErrorKey 的isValid

Tosh shimayama gave the right answer. $setValidity is a method from the NgModelController and takes two parameters: validationErrorKey and isValid.

更多关于 $ setValidity

改变的有效性的状态,并通知形式时,控制
  变化有效性。 (即,如果给验证是不通知的形式
  已标记为无效)。

Change the validity state, and notifies the form when the control changes validity. (i.e. it does not notify form if given validator is already marked as invalid).

出处和其他信息 AngularJS:NgModelController

这篇关于服务器端验证表单Angular.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆