1

I try to made nested form with validation. All works fine, but when I remove one of nested form, validation continue to use removed form. I made jsfiddle example http://jsfiddle.net/sokolov_stas/VAyXu/

When example runs, form are valid. If click "+" button, nested form will be added and valid will be false. Then click "-" button, and valid will be false all the same.

The question is: How to remove dynamic created form from validation processing.

ximage
  • 483
  • 1
  • 4
  • 7

1 Answers1

0

Well, for one thing, a <form> inside of a <form> is not valid HTML.

Second, you're not supposed to be doing DOM manipulation from inside the controller. The controller is for "business" logic. See the section on controllers here

For what you're doing, you'd probably be better off using one form, with an ng-repeat inside of it, and adding additional elements to an array:

<form name="myForm" ng-controller="FormCtrl" ng-submit="doSomething()">
   <div ng-repeat="item in items">
      <input ng-model="item" type="text" required/>
   </div>
   <a ng-click="addItem()">+</a>
   <a ng-click="removeItem()">-</a>
   <button type="submit">Submit</button>
   <div>Form valid: {{myForm.$valid}}</div>
</form>

and the controller:

function FormCtrl($scope) {
   $scope.items = [];
   $scope.addItem = function() { 
       $scope.items.push(null);
   };
   $scope.removeItem = function() {
       $scope.items.pop();
   };
   $scope.doSomething = function () {
       //your submission stuff goes here.
   };
}
Community
  • 1
  • 1
Ben Lesh
  • 107,825
  • 47
  • 247
  • 232
  • This is just example. In real world I have a large scale application. Forms adding in form by ng-repeat. I have to move my form to outside of main form. Now I have a nice logic like view stacks on mobile platform. All this because of have to edit items included in main item (editing FK linked rows). – ximage Oct 23 '12 at 06:48