【问题标题】:angularJS form validation only working for the first timeangularJS 表单验证仅适用于第一次
【发布时间】:2014-11-23 19:22:17
【问题描述】:

我正在使用 angularjs 进行表单验证,到目前为止我已经完成了 99%。但问题是我的表单验证仅在第一次提交时才正确。解释起来很混乱,但我会尽力解释:P。就是这样。当我重置表单时,它显示字段为空的错误消息(应该如此)。像这样

之后,如果我填写 2 个字段并再次提交,我将正常工作。就像这样

我的问题来了

我的代码

<form name="userForm" ng-submit="submitForm(userForm.$valid, user)" novalidate>


        <!-- NAME -->
        <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid && userForm.name.$dirty && submitted || (userForm.name.$invalid && userForm.name.$pristine) && submitted }">
            <label>Name*</label>
            <input type="text" name="name" class="item-input-wrapper" ng-model="user.name"  required>
            <label><p ng-show="submitted && userForm.name.$error.required" class="help-block "><font color="#009ACD">You name is required.</font></p></label>
        </div>

         <!-- EMAIL -->
        <div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && userForm.email.$dirty && submitted || userForm.email.$invalid && userForm.email.$pristine  && submitted  }">
            <label>Email</label>
            <input type="email" name="email" class="item-input-wrapper"ng-model="user.email"  required >
            <label><p ng-show="userForm.email.$invalid && userForm.email.$dirty && submitted || userForm.email.$invalid && userForm.email.$pristine && submitted" class="help-block"><font color="#009ACD">Enter a valid email.</font></p></label>
        </div>

        <!-- DESCRIPTION -->
        <div class="form-group" ng-class="{ 'has-error' : userForm.username.$invalid && userForm.username.$dirty && submitted || userForm.username.$invalid && userForm.username.$pristine && submitted }">
            <label>Description</label>
            <input type="text" name="username" class="item-input-wrapper" ng-model="user.username"  ng-minlength="5"  ng-maxlength="60" required>
            <label><font color="white"><p ng-show="userForm.username.$error.minlength && submitted"  class="help-block"><font color="#009ACD">Description is too short.</font></p></label>
            <label><p ng-show="userForm.username.$error.maxlength && submitted" class="help-block"><font color="#009ACD">Description is too long.</font></p></label>
            <label><p ng-show="submitted && userForm.username.$error.required" class="help-block "><font color="#009ACD">Description is required.</font></p></label>
        </div>


        <div class="col"style="text-align: center">
            <button align="left"class="button button-block button-reset"  type="reset" value="Reset" style="display: inline-block;width:100px;text-align:center "
            ng-click="submitted=false" padding-top="true">Reset</button>
            <button class="button button-block button-positive"  style="display: inline-block;width:100px "
            ng-click="submitted=true" type="submit" padding-top="true">Submit</button>
        </div>
    </form>
</div>

【问题讨论】:

  • 看逻辑:userForm.email.$invalid && userForm.email.$dirty &&提交|| userForm.email.$invalid && userForm.email.$pristine && 已提交 - 因为 $dirty 和 $pristine 是直接对立的,我原以为您可以将其简化为: userForm.email.$invalid && 已提交 || userForm.email.$invalid && 已提交。另外我认为 $dirty 和 $pristine 仅适用于整个 FORM 而不是单个 INPUT,但我承认我从未使用过它们。

标签: javascript html angularjs forms validation


【解决方案1】:

$setPristine();可能对您要查找的内容有用。尝试像下面这样添加...

<button class="button" type="reset" ng-click="form.$setPristine()">Reset</button>

有关更多信息和工作演示,请参阅以下链接

AngularJS does not proper handle button type="reset"?

您也可以参考以下链接将表单设置为原始状态

Reset form to pristine state (AngularJS 1.0.x)

【讨论】:

    【解决方案2】:

    找到解决办法

    在你的控制器里面添加这个

    $scope.reset = function() {
                $scope.user = angular.copy($scope.orig);
                $scope.userForm.$setPristine();
    
    };
    

    这里 'userForm' 应该是您的表单名称,而 'user' 应该是您的模型。

    【讨论】:

      【解决方案3】:

      这对我有用。试试吧。

      <form name="forms.sample" novalidate>
      
              <md-input-container class="md-block" flex-gt-sm>
                  <label>Name:</label>
                  <input ng-focus="focus=true" ng-blur="focus=false" style="width:400px;" class="form-control" name="Name" type="text" data-ng-model="Name" ng-pattern="SomePattern" required placeholder="enter the Name here" />
                  <div ng-messages for="forms.sample.Name.$error" ng-if="!focus">
                      <div ng-if='forms.sample.Name.$touched' ng-message="required">This is required.</div>
                      <div ng-if='forms.sample.Name.$touched' ng-message="pattern">Enter a valid domain name</div>
                  </div>
              </md-input-container>    
          </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多