I try to to use Backbone.validation plugin.
Here is a link to this plugin.
https://github.com/thedersen/backbone.validation.
I want to start a validation, when I create a new model instance. Normally in Backbone should implement a validate function and then when we should pass {validate:true}
How achieve the same result with this plugin?
//create picture instance in a controller var model = new Picture({ name: file.name, size: file.size, type: file.type }, {validate: true} );
//Picture class
export default Backbone.Model.extend({
defaults: {
name: "",
size: null,
type: ""
},
validation: {
size: function (size) {
if(size > this.MAX_FILE_SIZE;) {
return this.onFileSizeError()
}
return '';
},
onFileSizeError() {
//execute this when model size is wrong
}
});
Problem solved
So in a plugin documentation is information about validation on model without binding with a view.
So we can create a file with a base model which will be extend Backbone.Model.prototype. Now each new class should be extended by our class Model.
From now we can use a validation.plugin on our instance