Q:

Backbone: my validation is not stopping white space being set to value

I've been following a tutorial on Backbone and have my Tasks project setup here:
http://leongaban.com/_pro/
Now in my single Task View I had a validation bug, which I found out here that I needed to have {validation: true}
It works in that it prevents the user from entering in a null value (ie: deleting the current value) however I can't stop the user from entering only spaces
My Model and validate method:
App.Models.Task = Backbone.Model.extend({
    validate: function(attrs) {

        var errors = this.errors = {};

        if (attrs.title != null) {

            console.log('title is not null');

            if ($.trim(attrs.title) == null) {
                alert('error');
                console.log('title is required');
            }
        }

        // if ( $.trim(!attrs.title) ) {
        //  return false;
        // }
    }
});

This is where you get to set a new value:
editTask: function() {
        var newTaskTitle = prompt(this.msgPrompt, this.model.get('title'));

        // check for null value
        if ( !newTaskTitle ) return;

        console.log(newTaskTitle);
        this.model.set('title', newTaskTitle, { validate : true });
    },

If you notice in my example you can change the value and it works, or try and delete and submit a null value and the validate will stop it, however how would you efficiently not change the value if the user adds just white space?

A:

From the fine manual:

validate model.validate(attributes, options)
[...] If the attributes are valid, don't return anything from validate; if they are invalid, return an error of your choosing. It can be as simple as a string error message to be displayed, or a complete error object that describes the error programmatically.

The validate in your question never returns anything so it will never catch invalid values.
And $.trim works on strings, not booleans so I don't know what this:
if ( $.trim(!attrs.title) ) {
  return false;
}

is intended to do.
If your validate look more like this:
validate: function(attrs) {
    var errors = { };
    if('title' in attrs) {
        // Adjust this if you want to allow `null` values.
        if($.trim(attrs.title || '') === '') {
            errors.title = 'Missing title';
        }
    }
    return _(errors).isEmpty() ? undefined : errors;
}

then perhaps you'd get better results.
Demo: http://jsfiddle.net/ambiguous/p9ah5/
Some notes:

I use the in operator to see if attrs has a 'title' key with any value. The value could be null or false or 0 or 'pancakes' or ... we don't care what the value is, we just care if it is there so we use in to say exactly what we mean.
You seem to want the error messages in an object so we populate an object and use _.isEmpty to see if we put anything it, then we'll know what we should return.
Backbone will put the errors in validationError so there's no need for the this.error stuff.

