2

I have a form. Post submit, if the form is invalid, the error props out below the input fields.

One can hide the form using Cancel button.

The form can be displayed again using 'Show Form' button.

But the issue: The old form error still persists.

How can one reset the form without setting the ng-model associated with it as the input fields should be empty during load?

The reset, I should be able to do it from html itself and not from the controller.

Code below:

<form novalidate name="form.customForm">
      <div class="form-group">
        <div>
          <label>Name</label>
        </div>
        <div>
          <input type="text" name="name" ng-model="model.name" class="form-control" ng-required="true" />
          <span class="red" ng-show="(form.customForm.name.$touched && form.customForm.name.$error.required) || (form.customForm.name.$error.required &&     form.customForm.$submitted)">Name cannot be empty</span>
        </div>
      </div>

      <div class="form-group">
        <div>
          <label>Age</label>
        </div>
        <div>
          <input type="text" name="age" ng-model="model.age" class="form-control" ng-required="true" />
          <span class="red" ng-show="(form.customForm.age.$touched && form.customForm.age.$error.required) || (form.customForm.age.$error.required &&     form.customForm.$submitted)">Age cannot be empty</span>
        </div>
      </div>

      <button class="btn btn-primary" type="submit" ng-click="submit(form.customForm.$valid);">
        Submit
      </button>
      <button class="btn btn-default" type="button" ng-click="isForm = false;">
        Cancel
      </button>

</form>    

Refer the demo.

1
  • Why don't you try ngMessage Commented Aug 9, 2016 at 8:01

5 Answers 5

1

I would suggest you write the cancel button logic in the controller, are you sure you want to do it from the html itself?, you can use these statements to reset the form and fields.

form.customForm.$setPristine();
model = {};
form.customForm.$setUntouched();

The updated jsfiddle

Sign up to request clarification or add additional context in comments.

1 Comment

thankx, exactly this i was looking for. I have tried with form.customForm.$setPristine(); and model = {};, but didn't do the setting of $setUntouched().
0

On click on Cancel button, you could set form.customForm.$submitted = false;

This will hide the error messages.

Your cancel button becomes:

 <button class="btn btn-default" type="button" 
  ng-click="isForm = false; form.customForm.$submitted = false;">
        Cancel
 </button>

See jsfiddle

Comments

0

give the form a name:

<div ng-controller="BlaController as vm">
  <form name="vm.formOne">
  </form>
</div>

And in the controller do this: (thats how I made it work)

if (vm.formOne) {
  vm.formOne.$setPristine();
  vm.formOne.$setUntouched();
  vm.formOne.$commitViewValue();
}

Comments

0

what about just change the cancel button type to "reset" ?? It's the easiest solution

<button class="btn btn-default" type="reset" ng-click="isForm = false;">
     Cancel
</button>

3 Comments

But it does not work. When showing the form again, the error messages are still displayed See fiddle
in this case just add a cancel() function with "$setPristine();" as $scope.cancel = function(isValid) { $scope.form.customForm.$setPristine(); $scope.isForm = false; };
(cannot edit the previous) the cancel function should not have parameter
0
 $scope.cancel = function(form){
   $scope.isForm = true;
   form.customForm.$submitted=false;
   form.customForm.name.$touched = false;
   form.customForm.age.$touched=false;
  }

 <button class="btn btn-default" type="button" ng-click="cancel(form)" ng-show="!isForm">

Fiddle Demo

Or

<button class="btn btn-default" type="button" ng-click="isForm = true;
   form.customForm.$submitted=false;
   form.customForm.name.$touched = false;
   form.customForm.age.$touched=false;" ng-show="!isForm">

Fiddle Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.