mmmm
mmmm

Reputation: 3938

AngularJS validation and field name with square brackets

I'm following this tutorial http://scotch.io/tutorials/javascript/angularjs-form-validation but I have form fields with square brackets ( for example company_bundle_task[timeStart][date] - rendered by Symfony2 ) and validation on those fields doesn't work, for example this:

<span class="help-block" ng-show="company_bundle_task.company_bundle_task[timeStart][date].$invalid">Required</span>

or

<span class="help-block" ng-show="company_bundle_task.company_bundle_task[timeStart][date].$error.required">Required</span>

they are never shown, even though those fields are invalid/empty/etc.

Upvotes: 3

Views: 1195

Answers (2)

pdem
pdem

Reputation: 4076

You have to escape your name in quotes, since a.example is equivalent of a['example'] you can write:

<span class="help-block" ng-show="company_bundle_task['company_bundle_task[timeStart][date]'].$error.required">Required</span>

Upvotes: 7

Ch Faizan Mustansar
Ch Faizan Mustansar

Reputation: 364

Mati, You should try and use a function rather than evaluating such a long expression... call a simple function which is attached to your scope and controller and evaluate this expression correctly in it....

I usually, do not try to write the expression there but functions...

Regards

Upvotes: 0

Related Questions