angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Reset form $submitted state

Open juanluispaz opened this issue 12 years ago • 7 comments

Call to $setPristine() in a form don't reset the from submitted state, but the expected behavior must reset the submitted state.

Code for reproduce the problem:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example33-production</title>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular.min.js"></script>
  <style>.ng-submitted{background: red}</style>
</head>
<body ng-app="submitExample">
   <script>
   angular.module('submitExample', [])
     .controller('ExampleController', ['$scope', function($scope) {
       $scope.list = [];
       $scope.text = 'hello';
       $scope.submit = function() {
         if ($scope.text) {
           $scope.list.push(this.text);
           $scope.text = '';
         }
       };
       $scope.rs = function() {
         $scope.myf.$setPristine();
       }
     }]);
 </script>
 <form ng-submit="submit()" name="myform" ng-controller="ExampleController">
   Enter text and hit enter:
   <input type="text" ng-model="text" name="text" />
   <input type="submit" id="submit" value="Submit" />
   <button ng-click="rs()">reset</button>
   <pre>myform.$pristine: {{myform.$pristine}}</pre>
   <pre>list={{list}}</pre>
 </form>
</body>
</html>

juanluispaz avatar Aug 18 '14 16:08 juanluispaz

$setPristine does work correctly. There's two issues with your code:

  1. $scope.myf is not defined, replace it with $scope.myform
  2. A <button> element submits the form by default. So your "reset" button is actually also submitting the form.
    To get around this, you can use <input type="button" value="Reset" /> or <button type="button">Reset</button>. For more info, see button[type] on mdn

Here's your code with the above changes applied: http://plnkr.co/edit/wVZmcu0zuZTmjKa5YeWR?p=preview


In the future, try asking for help anywhere specified here before opening an issue on the issue tracker. If you had done it on IRC, you might have gotten a response instantly :-)

Closing this as work as expected.

rodyhaddad avatar Aug 19 '14 00:08 rodyhaddad

I'm not sure this is works as expected. The class ng-submitted is never removed from the form when form.$setPristine() is called. This leaves it in a different state than it started in.

After further investigation, I found that if form.$setPristine() is called from my controller right before state.go() (ui-router) is called that there is a race condition and ng-submitted class is not removed. If i use $timeout(function(){ form.$setPristine() }); then all works as expected.

kellyprankin avatar Feb 13 '15 01:02 kellyprankin

@kellyprankin thank you, your suggestion with $timeout helped me too!

gregor-srdic avatar Sep 24 '15 10:09 gregor-srdic

@kellyprankin I was running into the exact same scenario and issue. Looks like using $timeout is the only solution I've found so far. Not pretty, but it works!

calebjacob avatar Sep 29 '15 20:09 calebjacob

Thank for $timeout way )

zhdanovartur avatar Dec 02 '15 22:12 zhdanovartur

I don't understand why the sourcecode for form.$setPristine includes a line form.$submitted = false; if it doesn't remove the submitted state

neumn avatar Dec 22 '15 21:12 neumn

For me as well $timeout worked. I'm using AngularJS v1.4.9. However when I do some ajax and in callback of that ajax, I've to use $timeout, otherwise with only defined clearForm function, it works correctly.

app.controller('AddRoomTypeController', ['$scope', '$http', '$timeout', function($scope, $http, $timeout){
    $scope.roomType = {};

  $scope.addRoomType = function() {
    $.post('api/rooms', $scope.roomType).success(function(){        
        alert('Saved');
        $timeout(function(){ 
          $scope.roomType = {};
          $scope.frmAddRoomType.$setPristine();
          $scope.frmAddRoomType.$setUntouched();
        });

    });
  };

  $scope.clearForm = function() {
    $scope.roomType = {};
    $scope.frmAddRoomType.$setPristine();
    $scope.frmAddRoomType.$setUntouched();    
  };

}]);

Let me know if I am doing something wrong.

bharatpatil avatar Feb 02 '16 17:02 bharatpatil