emberjs/data — Issue #134: Issue with clearing bound errors
Author: mkocher
Created: 2012-03-01 20:10:58+00:00
State: open

We're setting up bindings to model errors, and seeing that they don't get cleared out when the field is set. The key gets deleted, but a getPath still sees the old value.

We ended up adding this to line 354 of states.js, but don't think it's the right thing to do permanently:

``` javascript
model.setPath('errors.' + key, undefined);
delete errors[key];
```

The delete is still necessary to trigger the object becoming empty and existing the error state.

We copied the test from line 477 in store_adapter_test.js and modified as following:

``` javascript
test("if a created model is marked as invalid by the server, it enters an error state", function() {
adapter.createRecord = function(store, type, record) {
equal(type, Person, "the type is correct");

if (get(record, 'name').indexOf('Bro') === -1) {
store.recordWasInvalid(record, { name: ['common... name requires a "bro"'] });
} else {
store.didCreateRecord(record);
}
};
var yehuda = store.createRecord(Person, { id: 1, name: "Yehuda Katz" });
store.commit();

equal(get(yehuda, 'isValid'), false, "the record is invalid");

set(yehuda, 'updatedAt', true);
equal(get(yehuda, 'isValid'), false, "the record is still invalid");

Ember.bind(yehuda, 'boundNameErrors', 'errors.name');
Ember.run.sync();
equal(get(yehuda, 'boundNameErrors'), getPath(yehuda, 'errors.name'), 'precond - should bind the bound errors');

var errors = get(yehuda, 'errors');
errors['other_bound_property'] = undefined;
set(yehuda, 'errors', errors);
set(yehuda, 'name', "Brohuda Brokatz");

equal(get(yehuda, 'isValid'), true, "the record is no longer invalid after changing");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");

equal(get(yehuda, 'isNew'), true, "precond - record is still new");

equal(getPath(yehuda, 'errors.name'), undefined, "should clear out the errors on name");

store.commit();
equal(get(yehuda, 'isValid'), true, "record remains valid after committing");
equal(get(yehuda, 'isNew'), false, "record is no longer new");

// Test key mapping
});
```