0

With AngularJS, I'm trying to build dynamically a form from a json.

Fiddle : http://jsfiddle.net/586pB/2/

You can see in the fiddle that :

  • generating form is ok
  • auto disabling submit button is ok

But, input validation doesn't work.

ng-class="{ 'has-error' : entryForm[field.id].$invalid && !entryForm[field.id].$pristine }"

This code is working when it's not in a loop : entryForm.title.$invalid. But not with this syntax : entryForm[field.id].$invalid

Thanks for your help.

Edit : solved Found my answer here : https://github.com/angular/angular.js/issues/1404 Each form-group is now a form, and each input has a static name.

odupont
  • 1,946
  • 13
  • 16
  • possible duplicate of [Dynamic validation and name in a form with AngularJS](http://stackoverflow.com/questions/14378401/dynamic-validation-and-name-in-a-form-with-angularjs) – odupont Feb 11 '14 at 17:49

2 Answers2

0

If you want add error styles to all input-fields in the form (if data on the form not correct), you can do next:

ng-class="{ 'has-error' : entryForm.$invalid}" 

instead

ng-class="{ 'has-error' : entryForm[field.id].$invalid }"

I update your jsfiddle with correct validation and button-disabling.

Update: JSFiddle

Artyom Pranovich
  • 6,814
  • 8
  • 41
  • 60
0

In angularJS, your form get parsed before ng-repeat.

So all your form input control get register with name '{{field.id}}' instead of its original value like title or position. That's why your condition for error class is not validating.

To solve this problem, you can create your custom directive with your field data.

dhavalcengg
  • 4,678
  • 1
  • 17
  • 25
  • 1
    Thanks, but I've found a better solution here : https://github.com/angular/angular.js/issues/1404 – odupont Feb 11 '14 at 17:14