Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I encounter a problem when testing form validation with angularjs

According to angularjs form guide,

an input control that has the ngModel directive holds an instance of NgModelController. Such a control instance can be published as a property of the form instance using the name attribute on the input control.

I created test code at plunker, it all works fine untill I change the input name from

<input type="number" name="age" ng-model="user.age" max="100" required>

<p>{{form1.age.$error}}</p>

to

<input type="number" name="user[age]" ng-model="user.age" max="100" required>

<p>{{form1.user[age].$error}}</p>

Does this mean angular can not recognize array syntax in form input?

The problem for me is I want to keep the normal form submission flow and only use angular for form validation, so I need to keep form input as array to work with the backend form handling

share|improve this question
1  
What are you trying to gain by setting a dynamic name on an element? –  Brocco yesterday
    
@xiaopang: Feel free to also upvote my answer if you really liked it :) –  ExpertSystem 7 hours ago
add comment

1 Answer

up vote 1 down vote accepted

It has nothing to do with Angular. It is a syntactic JS error.

If you want to reference a property named user[age], you should do it like this:

form1['user[age]'].$error

form1.user[age] is incorrectly interpreted as (form1.user)[age]

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.