thedersen/backbone.validation — Issue #32: When model is invalid, model.set('attr', 'value') will not stick even if the property is valid
Author: davisford
Created: 2012-03-08 03:37:45+00:00
State: open

Sorry for the confusing title, but I'm having an issue that may be just backbone.js or it may be backbone.validation...to try to put it simply, let's say I have a model like this:

```javascript
ED.Models.Time = Backbone.Model.extend({
defaults: {
type: 'Time',
connections: []
},
validation: {
operator: { required: true, msg: ED.Errors.operator },
time: { required: true, msg: ED.Errors.time },
connections: function (val) {
if (!val || val.length === 0) { return ED.Errors.connections; }
}
}
});
```

So, initially, connections is an empty array, and that means it is invalid according to the validation rules. The other validation rules are for properties that are fed values through an html form. They are only required.

When I go to fill out the form, I have change events bound to all the form fields, and I'll do something like this:

```javascript
timeChanged: function () {
this.model.set('time', this.$('.ed-widget-timepicker')[0].value);
console.log(JSON.stringify(this.model.toJSON()));
}
```

So, initially, the view's model is invalid b/c it does not contain anything in the connections array. When I go to set the time, and trigger the timeChanged function above, you'll see that I try to set the time property on the model, but it does not take.

Immediately after, I dump the model's JSON out, and the time property was not set. It is only after I fix the connections array and add something to it, so that it validates properly, and THEN set the time, will the time property stick.

This is confusing to me, and I wonder if it is intentional (not allowing valid model properties to be set until after an unrelated property validates) ?