angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

dynamic element validation

Open hyusetiawan opened this issue 14 years ago • 235 comments

I have a problem with validating inputs that are generated dynamically. Here is the fiddle that shows the problem: http://jsfiddle.net/flsilva/8JzDb/17/

this is taken from: https://groups.google.com/forum/?fromgroups=#!topic/angular/qLeUWZVuQMI

the name is generated fine but it's not bound to the form scope. Because I can name it correctly but the form is not bound, it seems that this behavior is inconsistent.

Thoughts?

hyusetiawan avatar Sep 21 '12 22:09 hyusetiawan

is there a way to modify the decorator so the $name value tracks a dynamic name for an ngForm? Ive got a bunch of nested ng-forms and id like to be able to discriminate which are invalid from the parent form's $error object. is there another way to do this? As is, dynamic name sticks in the attribute but $name references the pre-interpolated val

pkpkpk avatar Jun 23 '14 03:06 pkpkpk

+1

sonicblis avatar Jun 24 '14 14:06 sonicblis

@idiotslowdown if u have nested ng-forms, u can check the errors of each one using the form name

<ng-form name="myForm1">
  <ng-form name="myForm2">
    <div ng-class="{'has-error': myForm2['myinput1'].$invalid}">
      //form element with dynamic name "myinput1"
   </div>
  </ng-form>
</ng-form>
console.log($scope.myForm2.$valid)
console.log($scope.myForm1.$valid)

I had no problem at all using dynamic names and nested forms (with the decorator ofc)

agusl88 avatar Jun 24 '14 15:06 agusl88

Here this is what I mean: http://plnkr.co/edit/icbjmZEJtzmey4TFJ9WQ?p=preview

Each failure returns $name: "{{subitem.tag}}". It would be nice to retrieve the names of invalid forms from the parent error object, while remaining agnostic to the form names themselves ahead of time

pkpkpk avatar Jun 25 '14 10:06 pkpkpk

@idiotslowdown I see it now, if u inspect the var $scope.itemForm u will see {{subitem.tag}} instead of the var for each nested form. The solution in the decorator above is for dynamic input names, not for dynamic form names. If you want to do that i'm afraid you'll have to get the html with the form names pre-rendered from the backend

agusl88 avatar Jun 27 '14 20:06 agusl88

+1

martinoss avatar Jun 29 '14 12:06 martinoss

+1

g00glen00b avatar Jun 30 '14 13:06 g00glen00b

+1

icylace avatar Jul 07 '14 00:07 icylace

+1

ravishivt avatar Jul 15 '14 01:07 ravishivt

+1

rinatio avatar Jul 16 '14 13:07 rinatio

+1, I would love to see this fixed.

It would've saved me a lot of time in my "in the deep end" Angular learning process!

acodrington avatar Jul 17 '14 09:07 acodrington

+1

cornel-masson avatar Jul 17 '14 10:07 cornel-masson

Had the same problem. I spent a few days researching and think found a solution. I put a 'ng-form' inside the 'ng-repeat'. Using the name 'ng-form' and name input in directive 'ng-show', the angular can differentiate the input names, eliminating the problem of validation. =)

http://jsfiddle.net/8JzDb/148/

viniciusmsv avatar Jul 17 '14 14:07 viniciusmsv

Hello, I've created a directive to fix this. I hope this helps.

var ngNameDirective = ['$interpolate', function ($interpolate) {
    return {
        priority: 9999,
        controller: ['$scope', '$attrs', function ($scope, $attrs) {
            var interpolatedName = $interpolate($attrs.ngName)($scope);
            if (interpolatedName) $attrs.$set('name', interpolatedName);
        }]
    };
}];

Using ngName directive instead of name attribute you can register form-controls (ngModels) and sub-forms (ngForms) dynamically.

rafagda avatar Jul 21 '14 12:07 rafagda

+1

It's f****g ridiculous that animation got hundreds of improvement (according to changelog) and we are unable to cleanly, properly render a dynamic form in angular. I'm starting to feel that it was a very bad choice 1.5 years ago to build a large enterprise application on top of this framework...

dropout avatar Jul 23 '14 06:07 dropout

@dropout that's a bit melodramatic. You can get this behaviour to work, it's just not "out-of-the-box". Might have to do some real work. Shock, horror, gasp!

kirkbushell avatar Jul 23 '14 06:07 kirkbushell

@kirkbushell This is not a melodrama, this is the 2978934 nth fix and workaround I'm applying because it fails to prove under large scale. And it's harder to maintain these fixes and workarounds when you switch to a newer version number. You know it's enterprise development not javascript zoo...

dropout avatar Jul 23 '14 07:07 dropout

@dropout sorry, but you must be doing it wrong. I've been running a large application for nearly 18 months, from angular js 0.6 and despite the initial cost of making it work, it hasn't failed since.

kirkbushell avatar Jul 23 '14 07:07 kirkbushell

@kirkbushell I wonder how large this application is if you can maintain it yourself.

dropout avatar Jul 23 '14 07:07 dropout

@kirkbushell @dropout seriously is there a good reason why this is being overlooked it's getting absurd the number of comments against this issue.

digitalgym avatar Jul 23 '14 07:07 digitalgym