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

1

2 Answers 2

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);
        }
    };
});
Sign up to request clarification or add additional context in comments.

Comments

0

Go through following link this will help.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.