Andy Johnson
Andy Johnson

Reputation: 693

Angular reset form not working

I am developing an application where I am using bootstrap and angular together. Form validation using angular and bootstrap is working fine but I have a problem to reset the form. I call $setPristine() but it doesn't seem to be doing anything.

Here is my html.

 <div class="cl-xs-12" style="margin-top:20px">
<div class="col-xs-3"></div>
<div ng-controller="AddUpdateUserController" class="col-xs-6">
    <form class="form-horizontal" role="form" id="addEditMemberForm" name="addEditMemberForm" ng-submit="addUpdateMember(member)" novalidate>
        <input type="hidden" id="mmeberId", name="memberId" ng-model="member.id">
        <div class="form-group required" ng-class="{ 'has-error': addEditMemberForm.firstName.$invalid && addEditMemberForm.firstName.$dirty}">
            <label for="firstName" class="control-label col-xs-4">First Name</label>
            <div class="col-xs-8">
                <input type="text" class="form-control" id="firstName" name="firstName" placeholder="First Name" ng-model="member.firstName" ng-pattern="/^[0-9A-Za-z]+$/" required>
                <div ng-show="addEditMemberForm.$submitted || addEditMemberForm.firstName.$touched" ng-messages="addEditMemberForm.firstName.$error">
                    <div ng-show="addEditMemberForm.firstName.$error.required" class="validationErrorMessage">First Name is required</div>
                    <div class="validationErrorMessage" ng-message="pattern">First name is invalid. Only Alphanumeric characters are allowed</div>
                </div>
            </div>
        </div>
        <div class="form-group required" ng-class="{ 'has-error': addEditMemberForm.middleName.$invalid && addEditMemberForm.middleName.$dirty}">
            <label for="lastName" class="control-label col-xs-4">Middle Name</label>
            <div class="col-xs-8">
                <input type="text" class="form-control" id="middleName" name="middleName" placeholder="Middle Name" ng-model="member.middleName" ng-pattern="/^[0-9A-Za-z]+$/" required>
                <div ng-show="addEditMemberForm.$submitted || addEditMemberForm.middleName.$touched" ng-messages="addEditMemberForm.middleName.$error">
                    <div class="validationErrorMessage" ng-message="required">Middle name is required.</div>
                    <div class="validationErrorMessage" ng-message="pattern">Middle name is invalid. . Only Alphanumeric characters are allowed</div>
                </div>
            </div>
        </div>
        <div class="form-group required" ng-class="{ 'has-error': addEditMemberForm.lastName.$invalid && addEditMemberForm.lastName.$dirty}">
            <label for="lastName" class="control-label col-xs-4">Last Name</label>
            <div class="col-xs-8">
                <input type="text" class="form-control" id="lastName" name="lastName" placeholder="Last Name" ng-model="member.lastName" ng-pattern="/^[0-9A-Za-z]+$/" required>
                <div ng-show="addEditMemberForm.$submitted || addEditMemberForm.lastName.$touched" ng-messages="addEditMemberForm.lastName.$error">
                    <div ng-show="addEditMemberForm.lastName.$error.required" class="validationErrorMessage">Last Name is required</div>
                    <div class="validationErrorMessage" ng-message="pattern">Last name is invalid. . Only Alphanumeric characters are allowed</div>
                </div>
            </div>
        </div>
        
    </form>
    <div class="row">
        <label>{{message}}</label>
    </div>
    <label ng-model="status"></label>
</div>
<div class="col-xs-3"></div>
</div>

Here is my Javascript:

angular.module('SiteControllerModule').controller('AddUpdateUserController', ['$scope', '$http', '$location', function($scope, $http, $location){     
    $scope.resetForm = function () {            
        $scope.member = null;
        $scope.addEditMemberForm.$setPristine();            
    }
}]);

When $setPristine is called, my form is still dirty and looks like it left behind my Bootstrap validation Error.

Form Still Dirty

Any help related to this is appreciated.

Thanks

Upvotes: 2

Views: 512

Answers (1)

Diego Polido Santana
Diego Polido Santana

Reputation: 1435

Try this too:

$scope.addEditMemberForm.$setPristine();
$scope.addEditMemberForm.$setUntouched();

Upvotes: 1

Related Questions