2.3 Validating Models
Data validation is an important part of web applications, and Backbone apps are no different. In this lesson, we will look at how to create the
validate method that Backbone uses to validate models.
1.Getting Started3 lessons, 13:56
2.Backbone Models7 lessons, 38:03
3.Backbone Collections6 lessons, 17:52
4.Backbone Views4 lessons, 26:08
5.Backbone Routers3 lessons, 11:19
6.Conclusion1 lesson, 01:36
2.3 Validating Models
It's quite likely that sometimes, when you're setting attributes on your Backbone models, you'll want to validate them in some way to make sure the value is acceptable for that property. To do this, you can add a validate method to your Backbone model. Now, by default, the Backbone model does not have a validate method. However, if you add one with the name validate, it will use it at different points in your application, which we'll talk about in just a second. So, this is a function that takes a set of attributes and it can also take some options if you want to pass those in, but most of the time you probably aren't gonna be using the options. And so, the way this validate function works, is that, in here you can do whatever you need to, to validate the attributes which come in this attrs property here, these attributes which have been added to the model. And, if they check out, you return nothing from this function. However, if there is a problem, then you return an error message of some kind. So for example, our book here probably has a published date, right? Let's make sure that it's added as a number. So we can say, if attrs we'll call it pub_date is how we'll expect it, or actually let's just say published, that sounds better. So if attrs.published, so if that exists, and the type of attrs.published does not equal number, we can return published should be a number. Now I want to point out something here. Notice that if we just had a type of attrs.published does not equal number, then if our model did not have a published attribute, this error would happen, meaning that a published attribute not only has to be a number but it is required on this model. So I don't want to make it required I just want to be a number if they do add one so that why I have this attrs do published which means if there isn't a published attribute nothing will be returned. Okay, so lets go ahead and give this a test drive. We'll come back to the browser here, lets refresh this, and lets create a new book. And to validate our model we can do book.isValid. So I can say book.isValid and you can see that true is returned because our book does pass all of our validation, nothing is returned from the validate method when it's called. Let's go ahead and add an attribute. So we could say book.set and let's set published to, we'll just set it to a string for now. So now if I say book.isValid you can see we have false return which means our book is not valid. To find out what the problem is we can say book.validationError. And this attribute returns that error message, so we can say that published should be a number. So let's go ahead and do this again, we can say book.set, and then we can say published. And this time we'll set this to, I don't know, we'll just set a date, like that. And there and now I can say book.isValid and you can see that we get true back. I should mention that if your model has a validate method like that, it will be automatically run when you try to save the model to the server. And we'll talk about syncing models between the server in the next lesson, but for now know that if your model doesn't validate, it actually won't let you save it to the server. Let's look at one more method of validation, which is validation upon setting that attribute, which you can do with an option. So if I say book.get published, you can see that right now that's set to 2001. Okay. Let's do book dot clear. And so now published is undefined. So let's do book.set. And let's set published, we'll set it equal to string for now. And we will also pass along an option object which says, validate equals true. And you can see that false is returned. And if I say book.get("published') you can see that it's undefined. We tried to set the published value equal to two. But since the value two did not pass validation the value did not actually get set. So if instead I was to set published here equal to 1990 for example, then you can see that instead of returning false it returns our object, and if we get that publish date you can see that it was set to 1990. So that is how you can do validation in your Backbone models.