This entry is part 4 of 14 in the series for A Complete Guide for Learning Backbone Js
- Introduction and Installation
- Defining Models in backbone js
- Adding Validations in models backbone js
- Explaining views in backbone js
- How to use templates in backbone js
- How to improve templates in backbone js
- Collections in backbone js
- Collection views in backbone js
- Template helpers in backbone js
- How to use namespace in backbone js
- How to handle dom events in backbone js and define your custom events (Live Demo)
- Routing in backbone js
So in previous post, we defined a person class with some default values in it. Now I want to add some validations like, Age can’t be negative, or name can’t be blank. Let’s start adding Validations in models in Backbone js.
What we have till now
So we have a person model i.e.,
Defining the first validation
Now you can validations by using validate function like this:
Fire up the console and fire these commands
Now, lets head back to chrome developer tools. In the console:
Displaying the error messages.
To see the errors messages, you have to bind a listen to the event to the person. A simple way to do that is:
Now if you say:
Output on Chrome Console
Cool. Let’s see what we have on the console.
Pretty simple right? Let’s move forward and see how to handle data presentation, i.e., views in Backbone js.
If you are facing any issues. Check out the source code files at github. I will be creating more and more directories in the same repo regarding each post. Still, if you have any doubts you can comment on the blog post itself, and I will try to reply back asap.