2

I am trying to generate form dynamically and use validation at the same time. The problem that I have now is that the input attribute name='{{key}}' is not interpolated by the validation.

<div ng-repeat="options in formObject">
     <input type='radio' ng-model='ranodm' name='{{key}}'>
</div>

I know that I should use ng-form if I want a dynamic form, however I am having hard time to understand it. I am adding my simple plnkr example of what I am trying to achieve. It would be nice if someone would show how to solve this using ng-form or other method.

http://plnkr.co/edit/faG89bmu18nNNODVRV3x

sarunast
  • 2,443
  • 3
  • 27
  • 37
  • possible duplicate of [AngularJS: Fields added dynamically are not registered on FormController](http://stackoverflow.com/questions/15843765/angularjs-fields-added-dynamically-are-not-registered-on-formcontroller) – moribvndvs Feb 13 '14 at 13:11

2 Answers2

2

I found an answer from reading through this SO post: Dynamic validation and name in a form with AngularJS

I ended up using the dynamicName directive to work around this problem so I could use a UUID of as the name of the field. Here's a plnkr and the directive I used. I can't take credit for the directive, I found it on that other post.

http://plnkr.co/edit/RFrRXp2kWkP1Mefwl3Kn?p=info

myApp.directive('dynamicName', function($compile, $parse) {
    return {
        restrict: 'A',
        terminal: true,
        priority: 100000,
        link: function(scope, elem) {
            var name = $parse(elem.attr('dynamic-name'))(scope);
            elem.removeAttr('dynamic-name');
            elem.attr('name', name);
            $compile(elem)(scope);
        }
    };
});
Community
  • 1
  • 1
Craig Squire
  • 2,141
  • 14
  • 13
0

Go through following link this will help.

http://www.benlesh.com/2013/03/angular-js-validating-form-elements-in.html

dhavalcengg
  • 4,678
  • 1
  • 17
  • 25