jrief / django-angular

Let AngularJS play well with Django
http://django-angular.awesto.com/
MIT License
1.23k stars 293 forks source link

Design pattern for Form Validation #192

Closed jhagege closed 9 years ago

jhagege commented 9 years ago

Hi,

Thanks a lot for this great package. I'm struggling to understand what is the best Design Pattern for Form Validation in Django + Angular.

So far those are my considerations: 1/ Using Form / ModelForm in combination with django-angular mixins and processing the form as usual on the Django side. 2/ Same as 1/ but when wanting to save the forms to the DB, we POST with an Angular service to a REST API endpoint (DRF for example), deserialize and save to the DB. Error handling in this case? 3/ Hardcoding the form in the Angular template and then validation it with DRF endpoint (thanks to serializer options).

I cannot find there is a consensus around best practices on this matter, I would be very glad to have your take on this.

Thank you so much for your time and consideration. Regards,

jrief commented 9 years ago

Hi @cyberjoac great question!

Currently, in one of my projects I use both approaches, 1/ and 2/. DRF is a really good framework and very well done, but it is too generic and does not focus on AngularJS. Therefore you can't create a real single source of truth, the Form class or the Model class which converts to an AngularJS self-validating form. With DRF you always have to adopt the form. On the other side that gives you some more flexibility when creating forms. I would discourage approach 3/

jhagege commented 9 years ago

Thank you so much for your fast answer! In the case 2/, how do you deal with error handling? How can you bind the errors you receive in JSON to the form.

In the case 1/ (sending the form through regular POST and processing it with some FormView or equivalent FBV, what happens with previous existing state on the page, since it gets regenerated (suppose that data got dynamically updated after having generated the Django template through Angular scope). In this case, I think that the data will be reseted after submitting the form, correct? Any solution to deal with those issues?

Thank you so much again !

jrief commented 9 years ago

Well, I have to correct myself, approach /2 is not suitable for error handling. But in case you want to send the form data using Ajax/JSON instead of the classic multipart/form-data, django-angular can handle that too including error handling. So probably /1 is the best solution for you.