表单的服务器端验证 [英] Server-side validation for form

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

问题描述

表单的默认验证我按预期工作。但是,当用户键入有效的电子邮件地址和最少三个字符的密码时,这并不意味着登录凭据有效。

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.

所以我的问题是:


如何在$ b $之后将模型电子邮件密码设置为无效b 服务器端验证,因此输入字段获取类ng-invalid而非ng-valid。

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.

我当前的代码

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( '/' );
      }
    });
  };
}


推荐答案

Tosh 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

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

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