angularJS表单验证仅适用于第一次

时间:2014-09-29 11:49:57

标签: javascript html angularjs forms validation

我正在开发angularjs中的表单验证,到目前为止我已经完成了99%。但问题是我的表单验证工作只对第一次提交正确。这很难解释,但我会尽力解释:P。它就是这样。当我重置我的表单时,它显示错误消息,表明字段为空(应该如此)。像这样

enter image description here

之后,如果我填写2个字段并再次提交,我将正常工作,就像它应该的那样

enter image description here

现在我的问题来了。如果我重置我的表单(字段被清除)并提交表单,只显示描述字段错误,但不会显示其他两个。像这样

enter image description here

我的代码

<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>

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

找出了解决方案

在控制器内添加此

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

};

这里&#39; userForm&#39;应该是您的表单名称和&#39;用户&#39;应该是你的模特。

答案 2 :(得分:0)

这对我来说很有效。

<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>