Backend validation and callback

414 views
Skip to first unread message

sam

unread,
Sep 19, 2012, 10:17:39 AM9/19/12
to ang...@googlegroups.com
Hi, 

I wonder what is the best way to perform a backend validation over a model and display the errors in the view .
I know how to validate a form client side  but i also want to be able to it after a server response .

1 - fill a form
2 - in the controller use $http to post model data to a server
3 - server make some validation and return a json with errors if needed
4 - in the controller get the server response 
5 - display errors message to corresponding dom element ( the inputs ) 

I can't find any ressources about that , is there anybody to help and explain how to do this ?

Thanks.

sam

unread,
Sep 20, 2012, 12:10:31 AM9/20/12
to ang...@googlegroups.com
In fact i'm looking for how to do step 5 . 

Thanks

Pawel Kozlowski

unread,
Sep 21, 2012, 11:47:07 AM9/21/12
to ang...@googlegroups.com
Hi Samuel!

There were some discussions about doing validation based on $http
calls already but I guess there wasn't any complete example so I've
created one: http://jsfiddle.net/pkozlowski_opensource/butgr/17/

This is a simple form with a login input box and it will validate
logins against GitHub login names, so you can try it out (don't abuse
GitHub API thought :-)).

The way it works is rather straightforward: this is just a standard
ngModel validator that will do $http request to determine validation
results (assuming that a field is not valid waiting for the server's
response).

I hope it will get you started, but don't hesitate to ask if you've
got any further questions.

Also, if anyone got an idea on how to improve it I would be really
grateful for any input.

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>



--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

samuel merlet

unread,
Sep 24, 2012, 1:44:13 AM9/24/12
to ang...@googlegroups.com
Hi thanks for this exemple , from this i clearly understand how to make it for a field but how about validating a complete form with let's say 10 fields using only one request  and then display errors on each field based on the server response ?

I guess the directive need to be at the form level , but how to access each form input child to add errors message ?

Thanks again
Reply all
Reply to author
Forward
0 new messages