Validation messages not removed after resetting th

2019-08-17 16:35发布

I have a html form and I am using angular JS for validation. Here, after submitting the form I am calling the reset method which will reset the form input fields to default.

But, when I submit and call reset method, the validation messages appears in the input field. I have used the below code. I don't want to see the validation messages after submitting the form.

HTML

<form name="createvalidation" role="form" class="col-lg-12" novalidate>
    <div class="form-group">

    </div>
    <div class="form-group">
        <input type="text" placeholder="Challenge name" class="form-control input-sm" name="name" ng-model="challenge.challengename" required>
        <span ng-show="(createvalidation.name.$dirty || submitted) && createvalidation.name.$error.required">Name is reqiured</span>
    </div>

    <div class="form-group">
        <textarea class="form-control input-sm" rows="2" placeholder="Write more about this challenge..." name="description" ng-model="challenge.challengedescription" required></textarea>
        <span ng-show="(createvalidation.description.$dirty || submitted) && challengecreatevalidation.description.$error.required">Description is reqiured</span>
    </div>
</form>
<div>

    <button type="button" ng-click="createChallenge()">Create</button>
</div>

JS CODE

 $scope.createChallenge = function() {
     //get the field and store in db
     $scope.resetForm();
 }
 $scope.master = {};
 $scope.resetForm = function() {

     $scope.challenge = angular.copy($scope.master);
     $scope.createvalidation.$setPristine();
 }

2条回答
劳资没心,怎么记你
2楼-- · 2019-08-17 17:15

You appear to be copying the pristine state of the form from within the resetForm function. This means that it will only have the version of the form when resetForm is called. Instead perhaps you should be doing that copy whenever the form is setup and pristine.

查看更多
Lonely孤独者°
3楼-- · 2019-08-17 17:27
$scope.createChallenge = function() {
  //get the field and store in db
 $scope.resetForm();
 }

 $scope.master = {};
 $scope.resetForm = function() {
 $scope.submitted = false; //Try adding this
 $scope.challenge = angular.copy($scope.master);
 $scope.createvalidation.$setPristine();

}

查看更多
登录 后发表回答