Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Duplicate names of ngModel fields in form controller not supported. #12200

Open
ariesxje opened this issue Jun 24, 2015 · 9 comments
Open

Duplicate names of ngModel fields in form controller not supported. #12200

ariesxje opened this issue Jun 24, 2015 · 9 comments

Comments

@ariesxje
Copy link

In 1.0.0rc2, a form supports multiple fields to have the same name.
For example:

<form name="myForm">
    <div ng-repeat="item in items">
        <input ng-model="item.name" name="name">
    </div>
</form>

We can access the array of ngModelControllers by

{{myForm.name}}

But all versions after that, a form can only register a single modelController with the same name.
In the example above, myForm.name is the last item in the items array, which replaces all others.

Is there a way to access a certain group of fields on the form controller level like

{{myForm.name}}

?

I am trying to validate against uniqueness over multiple fields in a form.

@gkalpak
Copy link
Member

gkalpak commented Jun 24, 2015

You should use a different name for each field. Since you are using ngRepeat, you could use $index to create unique names. E.g.:

<form name="myForm">
  <div ng-repeat="item in items">
    <input ng-model="item.name" name="name{{$index}}">
  </div>
</form>

@Narretz
Copy link
Contributor

Narretz commented Jun 24, 2015

Since multiple form elements with the same name are accessible as an array in form.elements[inputName], maybe angular should do the same?

@ariesxje
Copy link
Author

I've found a way to achieve my goal anyway.
I was not aware that NOW Angular does support dynamic field name. Thanks, @gkalpak

But I still think it would be very helpful to access elements with the same name as an array.
Although it would bring confusion to the entire form scope while accessing a single field as it is not unique by name.

@booleanbetrayal
Copy link
Contributor

Definitely somewhat confusing when first encountering this issue. If there was an interpolation step in the ngModel -> form control name resolution then you could at least get around the issue usin $index in an ng-repeat, etc.

@gkalpak
Copy link
Member

gkalpak commented Aug 13, 2016

If there was an interpolation step in the ngModel -> form control name resolution

@booleanbetrayal, (as already mentioned above) there is.

@booleanbetrayal
Copy link
Contributor

Ahh, I was confused about some of the formController method parameters, and looks like they accept a NgModelController directly, so that's a fine workaround. Thanks!

@gkalpak
Copy link
Member

gkalpak commented Aug 13, 2016

Not sure what you mean, but if you're happy, I'm happy 😛

@AcarMeel
Copy link

Hi, I'm having the same issue. I got an ng-form inside of an ng-repeat. I got 2 inputs in this form ng-model="feat.Name" and ng-model="feat.Description". The thing is that all inputs are being updated when I type somehting. How can I avoid this behavior? I added to the inputs name=name{{$index}} and it didnt work, also I added that index to the ng-model but then the ng-repeat didnt work. Any help? thanks

@gkalpak
Copy link
Member

gkalpak commented Jan 30, 2017

@AcarMeel, this sounds like a general support question. Please, use one of the appropriate support channels for these types of questions.
GitHub issues are reserved for bug reports and feature requests.

Thx!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants