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

I'd be happy to. Is there anyplace that this is documented? I can't seem to find any clear definition of what the final implementation supports (in terms of syntax). Is there a working example somewhere?

DeborahK avatar Oct 31 '14 19:10 DeborahK

it's not documented anywhere, it's just the expected behaviour which is consistent with the way the framework actually works. The fact that it didn't work before was surprising to people.

The syntax is just interpolated strings, eg name="{{custom}}FormControl" or similar. This didn't used to work the way people expected, now it does, consistent with the rest of the framework.

I guess you've found that there is some documentation that it does not support this, so it would be good to remove or rewrite it to indicate the current behaviour.

caitp avatar Oct 31 '14 19:10 caitp

Thank you. Yes, minimally it should be removed. An example (maybe not here ... but with ng-repeat?) would be helpful.

For example, how do you reference this in something like ng-messages?

ng-messages="myForm.{{custom}}FormControl.$error"

does not work.

DeborahK avatar Oct 31 '14 20:10 DeborahK

Interpolated paths in ngMessages is a different problem, although it could certainly receive a similar fix.

caitp avatar Oct 31 '14 20:10 caitp

@DeborahK maybe you van just use ng-messages="myForm[custom + 'FormControl'].$error"

t8g avatar Nov 03 '14 15:11 t8g

+1 on this issue. Being able to pass name arguments to directives as part of their isolate scope would make life much easier.

Currently having to hodge : http://stackoverflow.com/a/25652866 into every application that requires dynamic validation and it just feels hacky.

AJMcKane avatar Dec 11 '14 15:12 AJMcKane

@scouser3008 this issue was fixed, you don't need to decorate ngModelController at all now. The name attribute is interpolated now.

caitp avatar Dec 11 '14 15:12 caitp

@caitp Yup, completely my fault flagging this. For IE compatibility I'm having to use v1.2 and got caught up in debugging! Apologies.

AJMcKane avatar Dec 11 '14 16:12 AJMcKane

@caitp I wish I found this thread a year ago when I first had this issue. I used the ng-form hack. Unfortunately, this hack does not work when your dynamic forms that are being repeated several times have radio buttons... because even though the form validation "works", now all of your radio buttons are part of the same radio group and so you can only select 1 on the entire page of forms. But I digress...

We are stuck on 1.2.x for the moment and so my question is, did @Thinkscape 's decorators (found above) prove to be as effective as the eventual solution that is implemented in 1.3.x?

spencerb02 avatar Jan 29 '15 22:01 spencerb02

@spencerb02 you mean the decorators from my plunker? :p

So, the stuff that was merged into 1.3.x has some extra meat to it. In particular, it will remove controls from the form controller and re-add them if the interpolated value changes, and the goal is to remove them when they are destroyed, too.

The decorators just interpolate one time, so it can cause problems during ngRepeat if an $index changes, for instance.

caitp avatar Jan 29 '15 22:01 caitp

@caitp excellent. Thank you. Forgive my naivety, but when would indexes change that wouldn't also be a redraw? Also, if I have another unique key to tie it to rather than $index, this won't be an issue, right?

spencerb02 avatar Jan 29 '15 22:01 spencerb02

@spencerb02 it's not exactly that simple, if the form is outside of ngRepeat, and the length changes, you can still have stale stuff in the form --- during a redraw, if an element is moved, it is not re-transcluded. http://plnkr.co/edit/rRYgWgdo4M9sUnGiNwqv?p=preview << so, the controller wouldn't be re-created, and it wouldn't create a new scope, just the scope values would change.

caitp avatar Jan 29 '15 22:01 caitp

+1

elyadarm avatar Mar 06 '15 14:03 elyadarm

+1

gaprl avatar Apr 26 '15 06:04 gaprl

+1

willfaught avatar May 15 '15 20:05 willfaught

+1

okev avatar May 20 '15 07:05 okev

+1

andreaslarssen avatar Jun 18 '15 08:06 andreaslarssen

+1

fouksf avatar Jun 19 '15 11:06 fouksf

+1

raphaelyancey avatar Jun 22 '15 08:06 raphaelyancey

+1

ymartoglio-plb avatar Jul 28 '15 15:07 ymartoglio-plb