2

I'm interested in validating form (all the fields) on the server side and then sending list of errors back to the client to be displayed. So validation would have to happen at a form level, but validity setting would have to be set per field. I'm not sure how to achieve it, any advices?

1 Answer 1

4

AngularJS provides rich custom validation framework. It exposes the form object within the controller scope and that object has different properties like $error, $dirty, $pristine etc that can be leveraged to create a custom validation solution. Basically all you have to do is get the validation errors from the server and set those properties accordingly.

In your template you would have

<form id="myForm" name="myForm" novalidate ng-controller="myController">

In your controller you would have

scope.myForm.$errors //and so on.

http://docs.angularjs.org/guide/forms

Also use this Chrome extension to inspect the form object at runtime https://chrome.google.com/webstore/detail/angularjs-batarang/ighdmehidhipcmcojjgiloacoafjmpfk?hl=en

Sign up to request clarification or add additional context in comments.

7 Comments

Do you knwo how to handle validation with form fields that are repeating in ng-repeat? Should I use subforms with ng-form?
I haven't done it, but if you inspect the form object that is created on the controller, you will know where to put the appropriate error messages.
@Katan: I'm not so sure.. I can't figure out if I need to use nested forms or not...
In html you do not have a form within a form, AFAIK. You can how ever use ng-repeat on a collection inside template and use input elements in those. If you post a JSFiddle of your app, we can play around.
@Katan: Ok, give me few minutes to prepare. Btw. Are you on freenode?
|

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.