1

So, I've used AngularJS on a few projects now (most recently a MEAN stack project). One part of the project that I wasn't particularly pleased with was the handling of input validation.

I'm soon going to kick off another project which with an AngularJS SPA, communicating with an ASP.NET Web API.

If I have a few requirements:

  • Simple input validation throughout (required fields, numerical ranges, date ranges, etc)
  • A need for the same validation to be applied through the API and in the AngularJS client
  • e.g. a 3rd party client should have the same input validation rules that our AngularJS client has, without the need to (attempt to) replicate them.

How are people solving this, without duplicating input validation logic?

It feels real bad to write all this validation logic twice - once in Web API land, and once in AngularJS land. I've got a few ideas about how to do this, but I'm keen to get some suggestions.

Thanks.

1 Answer 1

2

I think you're looking for Fluent Validation with jquery unobtrusive. There's a post describing using this with the Web Api here : https://brettedotnet.wordpress.com/2013/05/01/asp-net-web-api-validation-a-one-more-better-approach. Which essentially renders out some of the rules as attributes in the HTML. It does require an ajax post for some validation (I think this is mainly do to some conditional validation handled in the back end), but if the idea is to avoid rewriting them in the client side, it might do the trick.

You may also be able to plug in a .NET fluent validation library here: https://github.com/JeremySkinner/FluentValidation

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

Comments

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.