【问题标题】:Angular form is $valid when all inputs are blank, why?当所有输入均为空白时,角度形式为 $valid,为什么?
【发布时间】:2015-06-15 03:33:46
【问题描述】:

即使我的所有输入字段都是空白的,我的表单仍显示为有效。 我在输入字段中有必需的关键字。

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.1/css/materialize.min.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript" src="/components/angular/angular.min.js"></script>
    <script type="text/javascript" src="/js/app.js"></script>
  </head>

  <body>
    <main>
      <div class="container">
        <div class="row">
          <section ng-controller="LogInController as loginCtrl">
            <form name="signUpForm" action="/createuser" method="POST" novalidate>
              <fieldset>
              <div class="field input-field">
                Email <br />
                <input type="email" name="email" required />
              </div>

              <div class="field input-field">
                Password <br />
                <input type="password" name="password" required />
              </div>

              <div class="field input-field">
                Confirm Password <br />
                <input type="password" name="confirmation" required />
              </div>

              !! -- Form valid? {{signUpForm.$valid}} -- !!

              <div class="actions">
                <input type="submit" value="Sign Up" />
              </div>
              </fieldset>
            </form>
          </section>
        </div>
      </div>
    </main>
  </body>
</html>

在浏览器中加载它会导致 !! -- 表格有效吗?真的 - !! 我以为 Angular 知道必需的标签会使空白字段无效?

【问题讨论】:

  • 您在表单上有novalidate
  • @maddog novalidate 是必需的,因此浏览器不会执行验证。
  • @maddog 只是为了避免浏览器自己的验证弹出窗口,就像 chrome 在必填字段上所做的那样

标签: angularjs forms validation required angularjs-forms


【解决方案1】:

您应该在每个字段上放置ng-model 以在每个字段上启用表单, 除非您添加 ng-modelname 值,否则您的字段将永远不会被视为表单的一部分。并且做改变一件事创建一个父变量,例如form并在其中添加所有范围变量。就像在控制器中做 $scope.form = {}; & 然后在 UI 上添加所有 ng-model s 到它像 form.email, form.password & form.confirmation

为了获得更好的表单验证,请从表单中删除 actionmethod 属性并使用 ng-submit 指令,该指令将调用控制器方法之一。 & 请通过检查表单是否为$valid 从该控制器方法调用帖子。

标记

<form name="signUpForm" ng-submit="submit(signUpForm, form)" novalidate>
    <fieldset>
        <div class="field input-field">
            Email
            <br />
            <input type="email" name="email" ng-model="form.email" required />
        </div>

        <div class="field input-field">
            Password
            <br />
            <input type="password" ng-model="form.password" name="password" required />
        </div>

        <div class="field input-field">
            Confirm Password
            <br />
            <input type="password" ng-model="form.confirmation" name="confirmation" required />
        </div>

        !! -- Form valid? {{signUpForm.$valid}} -- !!

        <div class="actions">
            <input type="submit" value="Sign Up" />
        </div>
    </fieldset>
</form>

控制器

$scope.submit = function(form, formData) {
    if (form.$valid) { //submit form if it is valid
        $http.post('/url', {
          data: formData
        })
        .success(function() {
          //code here
        })
        .error(function() {
        })
    } else {
        alert("Please fill all required fields")
    }     
}

希望这已经清除了您对形式的小概念,谢谢。

【讨论】:

  • 正如您正确陈述的那样,每个&lt;input/&gt; 都应该获得一个ng-model 属性才能使验证工作(验证器被添加到模型中)。然而,您在第二次编辑答案时删除了这些属性……?