Daniel
Daniel

Reputation: 4342

backbone model sets value on false validate

I'm learning backbone.js and trying to figure out how to use the validation method. But my problem is when I call for validation the validation is triggered but it still saves the invalid data. As seen here player.set('firstName', '', { validate: true }); I set lastName variable to '' and it saves that values, and it shouldn't.

Edit - Updated code so now it works correctly Backbone model.

var Player = Backbone.Model.extend({
    defaults: {
        firstName: "Not specified",
        lastName: "Not specified",
        position: "Not specified"
    },
    validate: function (attributes) {
        // validate first name
        if (!attributes.firstName || attributes.firstName === '') {
            return 'you must have a first name';
        }
        // validate last name
        if (!attributes.lastName || attributes.lastName === '') {
            return 'you must provide a last name.';
        }
        // validate position
        if (!attributes.position || attributes.position === '') {
            return 'you must provide a position.';
        }
    },
    initialize: function () {
        // checks when firstName has changed
        this.on('change:firstName', function () {
            console.log("firstName attribute has changed.");
        });
        // checks when lastName has changed
        this.on('change:lastName', function () {
            console.log("lastName attribute has changed.");
        });
        // checks when position has changed
        this.on('change:position', function () {
            console.log("position attribute has changed.");
        });
        // check for valid inputs
        this.on('error', function (model, error) {
            console.log(error);
        });
    }
});

From within chrome dev tools I do the following code.

var player = new Player;
player.set({ firstName: "peter", lastName: "andrews", position: "outfield" }, { validate: true });
player.toJSON();
firstName attribute has changed. app.js:32
lastName attribute has changed. app.js:36
position attribute has changed. app.js:40
Object {firstName: "peter", lastName: "andrews", position: "outfield"}

player.set('firstName', '', { validate: true });
player.toJSON();
you must have a first name app.js:14
Object {firstName: "peter", lastName: "andrews", position: "outfield"}

Upvotes: 1

Views: 243

Answers (1)

max
max

Reputation: 6069

From the Backbone docs:

If the attributes are valid, don't return anything from validate; if they are invalid, return an error of your choosing.

Your validation function is printing the error strings with console.log but not returning anything.

Try changing your validate function to something like:

validate: function (attributes) {
    // validate first name
    if (!attributes.firstName || attributes.firstName === '') {
        return 'you must have a first name';
    }
    // validate last name
    if (!attributes.lastName || attributes.lastName === '') {
        return 'you must provide a last name.';
    }
    // validate position
    if (!attributes.position || attributes.position === '') {
        return 'you must provide a position.';
    }
},

Upvotes: 2

Related Questions