Usually, on frontend applications, the inputs are validated with UI plugins such as jQuery Validation, which focuses on the user interface. In other words, the data is validated directly on the DOM. However, on bigger applications that would not be the best approach.
Validations in Backbone can be done manually or through plugins. Of course, the best approach is to use a plugin because it saves time and effort, but before we learn how to use the backbone.validation
plugin, I want to show you how native validation works.
Backbone models have three properties to help us validate the model data: validate()
, validationError()
, and isValid()
. The validate()
method should return nothing if the model data is correct, or a value otherwise.
Backbone leaves open what should be returned by the validate()
method, so you can return just a plain string message or a complex object:
class Chapter extends Backbone.Model{ validate(attrs, options) { if (attrs.end <...