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

Thank you to the AngularJS devs for the changes apparently made in 1.3 because I had no trouble getting validation to work for dynamically-added form elements using ngRepeat in AngularJS 1.4.3

To summarize a slightly ambiguous discussion from earlier this year, AngularJS 1.3 now allows you to dynamically assign the name inside ngRepeat using the double curly braces.

name="formControl_{{uniqueId}}"

To reference the form element name in a more complicated path, use this pattern:

ng-messages="myForm[ 'formControl_' + uniqueId ].$error"

There is no longer a need to use subforms (embedded ngForm directives) to make validation work for dynamically added form elements - tested using AngularJS 1.4.3

jeremyml avatar Jul 29 '15 02:07 jeremyml

+1

oddlyfunctional avatar Aug 28 '15 23:08 oddlyfunctional

+1 as @jeremyml said about complicated paths, i ended up using the next path to access a dynamic field inside of an ng-form form, inside of an ng-repeat

mainForm['extraInfoForm_' + $index]['name_' + $index].$error

What a workaround!

Angular version 1.4.5

juancarlosqr avatar Sep 02 '15 19:09 juancarlosqr

Thanks @rafa-gda for the ng-name directive! Works like a charm on 1.4.5

agentGreg avatar Sep 17 '15 23:09 agentGreg

I'm glad to hear it @matfiz but you do not need that workaround in v1.4.5 since the form directive interpolates the provided name by default. If you use name attribute instead of ng-name in your form it should work just fine.

rafagda avatar Sep 21 '15 06:09 rafagda

@rafa-gda, thanks! But I am using it on input fields, so I can handle more complex validations on nested, dynamically added form elements (conditions on multiple fields). The name attribute in v1.4.5 on input did not interpolate, I had to use your hacky ng-name :)

agentGreg avatar Sep 21 '15 07:09 agentGreg

@matfiz, input fields support interpolated name attributes since v1.3.0-beta.11 (so all stable 1.3.x and 1.4.x versions). Maybe the problem lies somewhere else.

gkalpak avatar Sep 21 '15 10:09 gkalpak

@gkalpak, thanks for the useful remark! You were right, thanks to your information I have found an issue elsewhere

agentGreg avatar Oct 06 '15 05:10 agentGreg

Is this fixed? I am trying to use input directives but the validations aren't working as expected =\

jodinathan avatar Dec 31 '15 12:12 jodinathan

@jodinathan, yes this has been fixed. If you are having problems using dynamic element names, I would suggest creating a live demo of the issue and posting it to one of the appropriate support channels.

Or, if you think you found a bug/regression, please open a new issue with a detailed description (and preferrably a live demo - e.g. using CodePen, Plnkr etc).

gkalpak avatar Jan 22 '16 16:01 gkalpak

is this issue fixed ? I am struggling same issue . Could you please work around other than nested ng-form

diwanoli avatar Jun 03 '16 22:06 diwanoli

I am getting below error when i use {{}} symbol in ng-messages directive

Error: [$parse:syntax] Syntax Error: Token '{' is an unexpected token at column

- [<select-ist-stake name="istStakeRole{{$index}}" ng-model="team" ng-required="true"></select-ist-stake>
                        <span class="help-block" ng-messages="stakeHolderForm.istStakeRole$index.$errors" ng-show="stakeHolderForm.$submitted || stakeHolderForm.istStakeRole$index.$touched || stakeHolderForm.istStakeRole$index.$error" >
                            <span ng-messages-include="common/components/validations/validationMessages.tpl.html"></span>
                        </span>
 ] 

diwanoli avatar Jun 03 '16 23:06 diwanoli

@diwanoli, if you think this is a bug, please open a new issue providing all necessary details (ideally a runnable demo of the problem). If you are not sure if this is a bug in Angular, try asking a question on one of the support channels.

gkalpak avatar Jun 06 '16 11:06 gkalpak

+1

sajjadabdi avatar Feb 22 '17 16:02 sajjadabdi

+1

sajjadabdi avatar Feb 22 '17 16:02 sajjadabdi