3

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.

3
  • Can you please write the code of the input fields? The PHP result rendered by symphony should be mapped on the "model" and the validation on the "name" wich can be different. Commented Apr 15, 2014 at 12:30
  • <input type="date" ng-model="task.date" maxlength="255" required="required" name="company_bundle_task[timeStart][date]" id="company_bundle_task_timestart_date" class="ng-pristine ng-invalid ng-invalid-required"> Commented Apr 15, 2014 at 12:45
  • Thanks, so it seems that you don't need the 2 way data binding. Il will answer giving this info. Commented Apr 15, 2014 at 12:53

2 Answers 2

7

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

but when should I call it? after submitting the form? pros of my solution is that it works on focus/on blur ( just dynamically, you don't have to submit whole form )
No.. U should call the function in the ng-show, and it will be evaluated when the page is being rendered.. And also the expression which you have added in ng-show, will be evaluated while page rendering.
My advice is to add this expression in a function.. and then add a debugger in it and check why this expression is failing for your case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.