2

Being a developer who shifting to angular js from asp.net mvc, I am disappointed with the default validation implementation of angular js validation. It seems that angular, by default implement the client-side validation in the view and this violate the rules of separation of concern (imho).

Personally, I am more fond to implementing the validation rules in the viewmodel instead of view. Not sure whether this is widely adopted and recommended in angular js community.

So my question is:
1) Is the default implementation of validation in angular js a preferred way than separating it into view model?? 2) Is there any existing angular library that implement the validation logic in separate layer?

I am expecting something like this.

model.User = {
    Name: { validate: required, errorMessage: 'Name is required'},
    Price: {
        validate: number, errorMessage: 'Price should be number',
        validate: number.min = 0, number.max = 100, errorMessage = 'Price should be between 0 and 100',
    }
}

<input ng-model="User.Name" name="Name">
<input ng-model="User.Price" name="Price>
5
  • Part of the beauty of Angular is it's use of directives. One that you may be interested in would be ngMessages. Do you have a question or just venting? Commented Jan 24, 2016 at 7:13
  • looks like ngMessages is what you want Commented Jan 24, 2016 at 7:23
  • @BrianBaker i have edited the post to include my question.Yes, we can make use of directives, filters, etc to acheive it. But the fundamental question is that if there is no existing angular library that support this feature, we need to reconsider and think through why others do not implement the validation logic separately from view and why this is not been a practice in angular js before implementation as we are still in design phase. Commented Jan 24, 2016 at 7:23
  • @pixelbits Unfortunately, no. Our consideration is to wiped off ng-minlength, ng-maxlength, ng-message etc entirely from the view (during coding) but it should be generated from the view model during initial load of the page. Commented Jan 24, 2016 at 7:27
  • In angular, you declaratively place validation directives on an input control. You can then bind to certain model properties on scope, namely the form.$error or form.name.$error properties. Depending on the state of the $error object you can render any kind of messages using ngMessage. Imo, actually very similar to the way MVC handles it. Commented Jan 24, 2016 at 7:33

1 Answer 1

1

Anugular 2 has Model Driven Forms where you bring validation logic in controller.

Good to read-forms-template-driven-vs-model-driven

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

1 Comment

Thanks for the reply. At least we know that it has been supported now. At least, it is being proved that this is still a better way. But we will be using angular 1.4 for our development.

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.