I
I
IlgizVafin2015-03-14 14:38:43
JavaScript
IlgizVafin, 2015-03-14 14:38:43

AngularJS. What is the best way to organize field validation?

Hello!
AngularJS + ASP.NET Web API
The problem is that the validation rules for the fields must be described both on the client and on the server. Obviously, there is a difficulty in maintaining and "synchronizing" the validation logic between the client-server in a developing project.
Client-side form validation is implemented using standard directives: required, min, max, pattern, etc. + custom logic in its directives (dependencies between fields, for example).
There is no clear understanding of how to come to the optimal option, which will simplify the support of the validation logic. The ideal would be to have a structure in which all the logic would be concentrated in one place!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
FINTER, 2015-03-14
@IlgizVafin

The fact is that ideologically "server" validation and UX validation are two different things. UX validation is more likely to increase the convenience of filling in fields, and server-side validation is more likely to make a business decision. Part of the code can really be combined, but personally I always write separate logics for front and back. Yes, it's a little longer, but the devil is in the details =)

M
Mikhail Osher, 2015-03-14
@miraage

This is not possible in AngularJS. This is not a meteor/derby for you.

R
Razaz, 2015-03-14
@Razaz

1. Option - T4 templates that will automatically generate js with validation rules for your models. Unscrew something like ModelMetadataProvider and set it on your models. Well, or file a task for Gulp, which will scan the source code and generate it. We do this for localization. All resx through T4 are distilled into js and the same resources are used, without the need to duplicate something.
2. In runtime, you give validation rules to the client. Look towards Collection+Json. For WebApi there is https://github.com/WebApiContrib/CollectionJson.Net

I
Igor Kalashnikov, 2015-03-14
@zo0m

Maybe in conditions when you don’t have time to reach one after another, do 2 things.
1. leave a minimum of client validation. (here you can stop). Yes, it will be a little less convenient for users, but there will be no non-working forms.
2. Still it is possible as a temporary crutch, to make a web service\rest and to validate on the server. (only make timeouts so that not every user sneeze pulls such an operation)
------
A good answer was given by Finter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question