user1990553
user1990553

Reputation: 763

Backbone Validate does not work

I am using Backbone's validate function to guarantee Man to have an age property more than 18. Here is my code:

var Man = Backbone.Model.extend({
    initialize : function(){
        this.on("error",function(model,error){
            alert(error);
        });
    },
    validate : function(attrs,options){
        if (attrs.age < 18){
            return 'below 18';
        } 
    }
})

var man = new Man({name : 'qian', age : 12});

But looking at the result it seems that validate doesn't work.

Upvotes: 22

Views: 15306

Answers (4)

yesnik
yesnik

Reputation: 4695

For backbone v.1.0.0

var Man = Backbone.Model.extend({
    initialize : function(){
        this.on("invalid",function(model,error){
            alert(error);
        });
    },
    validate : function(attrs, options){
        if (attrs.age < 18){
            return 'below 18';
        } 
    }
});

Example 1. Without {validate:true}

//Object will be created with invalid attribute 'age' 
var man = new Man({name : 'qian', age : 12});
console.log(man) // Returns an object with invalid attributes

// But we'll use only valid objects.
// Also we'll get the error message in alert, if validation fails.
if(man.isValid()){
    alert( man.get('name') );
}

var man = new Man({name : 'qian', age : 19});
if(man.isValid()){
    alert( man.get('name') );
}

Example 2. With {validate:true}

//Object will be created without any passed attributes
var man = new Man({name : 'qian', age : 12}, {validate:true});
console.log(man) //Object will be without passed attributes

/* man.isValid() returns 'true' throw we passed invalid attrs.
   We won't see any error alert message, because Backbone created empty object */
/* Doesn't work */
if(man.isValid()){
    alert( man.get('name') ); //undefined
}
/* Works */
// Created model had invalid attrs, so validationError won't be empty.
// If all attrs are valid, validationError will be empty
if(!man.validationError){
    alert( man.get('name') );
}

Upvotes: 8

Akshay
Akshay

Reputation: 3866

If you are using new version(>1.0) of Backbone and want to fire validation at the time of model.set method,

then you must have to pass {validate: true} to fire validation.

use

model.set({field:value},{validate: true})

OR

model.set("field","value",{validate: true})

instead of

model.set({field:value})

REF : Backbone change log

Upvotes: 4

Tom
Tom

Reputation: 26849

In Backbone.js (prior to version 0.9.10), validate is called before save as well as before set.

You will get an alert error when you set invalid value.

Example - age value is below 18 :

var man = new Man ({name : 'qian', age : 12});
man.set({ age: 12 }); // that will trigger alert

EDIT

For Backbone.js version 0.9.10+ there is an issue reported: Failed validation does not trigger error callback. Issue explanation says that

invalid event should be used instead of error

So changing your code to:

var Man = Backbone.Model.extend({
    initialize : function(){
        this.on("invalid",function(model,error){
            alert(error);
        });
    },
    ...

And setting variable with validate option set to true will trigger an alert.

man.set({age: 12}, {validate : true});

Upvotes: 42

Vitalii Petrychuk
Vitalii Petrychuk

Reputation: 14255

var man = new Man({name : 'qian', age : 12}, {validate : true});

EDIT:

validate method works only in case if you pass an options object with validate param (from 0.9.9 version): https://github.com/documentcloud/backbone/blob/master/backbone.js#L539

And it triggers not error event but invalid event

Upvotes: 3

Related Questions