3

I am working on a new project, which includes several standard forms(Login, Registration, etc).

I have a basic client side validation with ng-required, type, and etc.

The problem is that I might get other kinds of errors from the Django REST backend, such as length, unique constraint and others, and those rules might change quite frequently.

The django REST server returns the errors in a JSON string, in shich the key is the field, and the value is the description of the error.

Is there anyway to write something in angular that will automatically append an error next to the invalid element, as a modular unit, that can be reused, so I won't have to add an error container and ng-bind for it per each field in my form?.

Thanks for any assistance.

1 Answer 1

1

When you want to write reusable code like this, your best choice is to use directives. You can create a directive named <email></email> and then inside the template, populate it with the input element and display the {{error}} next to it. There are several ways of getting the error into the directive template, but I'd suggest isolate scope and pass the data into the directive. This helps make directives more reusable.

If you've done things correctly then your backend 'Django REST' shouldn't have anything to do with this front-end functionality for the directive. All you need to do is change the data inside the controller and it will automatically change the data in the directive. So it gives a good level of abstraction as well.

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

2 Comments

Truth is that I am quite confused with AngularJs, I used a lot of jQuery on past projects. I am now spending my time in the reference which is also confusing, and some videos on Youtube. Any good online resource or book?
Check out the angularjs youtube channel, it has a great video on writing directives. Also check out Egghead Website

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.