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

@dropout I never said I was the only maintainer.

@digitalgym not sure tbh, seems like a likely candidate for a fix/feature addition.

kirkbushell avatar Jul 23 '14 07:07 kirkbushell

I think the subform validation should have some options where we can set all elements of a subform being pristine not to trigger an invalid form error even if some of these subform fields are required.

I am thinking to develop a very large applciation in angularjs, i love it, it is fun, fast easy and complicated at the same time.

I cant wait to @dropout and @digitalgym having some number line of code argument.

@dropout, there is a solution to this subform dynamic name fix a bit eralier in the comment. You just have to modify your app.config, and add some $provides, to override the default angular behavior concerning inputs dynamic name, so you can have

 <input name="firstname_{{i}}"> 

into a ng-repeat.

here is the link : http://jsfiddle.net/Thinkscape/23RPt/

a-lucas avatar Jul 23 '14 07:07 a-lucas

It's been a few months since I came up against this issue so my memory is a little hazy however here's my problem.

I have a form that has 'add another' functionality that dynamically adds new fields (3 fields) to a form, I also have an x to remove the fields. I want to access each 'subform' in a controller to perform validation actions based on the data provided by the user.

Some of the hacks in this thread have enabled me to add the new fields, but the names/indexes don't get updated upon delete so then the validation gets applied to the wrong fields, and I haven't found a way to access the inner forms individually in the controller to validate them.

Angular has a 'dynamic html' philosophy, but (unfortunately) it doesn't seem to extend to forms (yet).

j0hnsmith avatar Jul 23 '14 08:07 j0hnsmith

nested ng-form solves that issue.

kirkbushell avatar Jul 23 '14 08:07 kirkbushell

I'm pretty sure I tried that, iirc I couldn't access the nested forms in the controller (or didn't know how to). I'll have to put together a demo repo as my use case seems to be a little more advanced than the others I've seen.

j0hnsmith avatar Jul 23 '14 08:07 j0hnsmith

If you need to access it via controller, that can get tricky. We had to do a combination of directives and shared controllers to manage the fields and forms. Depends on what your'e trying to do. If it's just validation, you should be able to use the built-in angular (or your own custom validation rules) to manage that.

kirkbushell avatar Jul 23 '14 08:07 kirkbushell

@kirkbushell Excuse my ignorance, you've written "I've been running" not "maintaining"

To all the others having problems rendering complex dynamic forms against remote async validations, server generated access controlled configurations etc... my advice if you are about to start something like this roll your own thing, don't try to use angular forms and validation facilities for this because it's pretty immature and causes more problems than it solves... this whole damn thread speaks for itself

dropout avatar Jul 23 '14 11:07 dropout

@dropout you inferred on your own that I was a lone cowboy, I've corrected you with clarification. Case closed. Even without that, your implication with your statement was rather rude, rather than just asking ;)

And must disagree with dropout, as we've been able to get it going since earlier angularjs versions. Don't spread lies @dropout simply because you've had certain difficulties. We had our earlier problems, and as I said earlier it certainly does require some custom work, but it's possible, and it'll work once setup correctly.

It would be good, however - to have this baked into the codebase.

kirkbushell avatar Jul 23 '14 11:07 kirkbushell

@kirkbushell I'm not spreading lies it's my opinion. Sorry about that.

dropout avatar Jul 23 '14 11:07 dropout

+1

ernestopye avatar Jul 25 '14 18:07 ernestopye

+1

rodrigok avatar Jul 29 '14 13:07 rodrigok

@Rafa-GdA Sorry for my ignorance, but how can I use your directive? I don't realize how to implement your code as a directive into my code.

Can you help me? Thanks.

rodrigok avatar Jul 29 '14 13:07 rodrigok

@rodrigok Here's a fiddle with an example: http://jsfiddle.net/y3YJk/ And here another fiddle with the first post example using the directive: http://jsfiddle.net/8JzDb/149/

rafagda avatar Jul 29 '14 16:07 rafagda

Thank you @Rafa-GdA

rodrigok avatar Jul 30 '14 13:07 rodrigok

+1

adzhiljano avatar Aug 04 '14 15:08 adzhiljano

+1

bsr203 avatar Aug 10 '14 00:08 bsr203

+1

This appears to be the leading workaround: http://plnkr.co/edit/hSMzWC?p=preview

Are there any pitfalls or gotchas I might need to be aware of?

mrcoles avatar Aug 12 '14 18:08 mrcoles

+1

bolemeus avatar Aug 15 '14 15:08 bolemeus

I'm not sure of the exact details of the gotcha.

@matsko has said that they do this differently in AngularDart for some reason, because apparently it's easier to nest child forms underneath and do dynamic validation that way. But honestly, I don't really get why that's any simpler or easier to manage than this one. I've yet to encounter the pitfall with this approach.

caitp avatar Aug 16 '14 00:08 caitp

+1 after spending hours and hours trying to debug what i'm doing wrong, looks like it's a pretty popular pain point for the community ;). Hope to see a fix soon.

sgandotra avatar Aug 16 '14 14:08 sgandotra