Skip to content

Commit

Permalink
Validations on controller or model
Browse files Browse the repository at this point in the history
Not the correect direction for bubbling up validation model
but this is a good stop-gap for now.
  • Loading branch information
bcardarella committed Mar 18, 2013
1 parent a9ce85f commit 92b7ab7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/ember-easyForm/lib/views/error.js
Expand Up @@ -13,7 +13,7 @@ Ember.EasyForm.Error = Ember.View.extend({
} else {
return (this.get('controller.errors.'+this.property) || [])[0];
}
}.property('controller.content.errors.'+this.property);
}.property('controller.errors.'+this.property);
this.reopen(watchFunc);

this.set('template', Ember.Handlebars.compile('{{view.'+this.property+'Watch}}'));
Expand Down
13 changes: 9 additions & 4 deletions packages/ember-easyForm/lib/views/form.js
Expand Up @@ -3,17 +3,22 @@ Ember.EasyForm.Form = Ember.View.extend({
attributeBindings: ['novalidate'],
novalidate: 'novalidate',
submit: function(event) {
var object = this.get('context').get('content'), _this = this;
var _this = this, promise;

if (event) {
event.preventDefault();
}

if (object.validate === undefined) {
if (Ember.isNone(this.get('context.validate'))) {
this.get('controller').send('submit');
} else {
object.validate().then(function() {
if (object.get('isValid') === true) {
if (!Ember.isNone(this.get('context').validate)) {
promise = this.get('context').validate();
} else {
promise = this.get('context.content').validate();
}
promise.then(function() {
if (_this.get('context.isValid') === true) {
_this.get('controller').send('submit');
}
});
Expand Down
8 changes: 6 additions & 2 deletions packages/ember-easyForm/lib/views/input.js
Expand Up @@ -45,8 +45,12 @@ Ember.EasyForm.Input = Ember.View.extend({
return '{{errorField '+this.property+' '+options+'}}';
},
focusOut: function() {
if (this.get('context').get('content').validate) {
this.get('context').get('content').validate(this.property);
if (!Ember.isNone(this.get('context.validate'))) {
if (!Ember.isNone(this.get('context').validate)) {
this.get('context').validate(this.property);
} else {
this.get('context.content').validate(this.property);
}
}
}
});
4 changes: 2 additions & 2 deletions packages/ember-easyForm/tests/helpers/formFor_test.js
Expand Up @@ -24,10 +24,10 @@ module('the formFor helper', {
lastName: 'Cardarella',
errors: Ember.Object.create()
});
controller = Ember.Controller.create();
controller = Ember.ObjectController.create();
controller.set('content', model);
controller.set('count', 0);
controller.set('submit', function() { this.set('count', this.get('count') + 1); });
controller.submit = function() { return this.incrementProperty('count'); };
},
teardown: function() {
Ember.run(function() {
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-easyForm/tests/helpers/input_test.js
Expand Up @@ -6,7 +6,7 @@ var original_lookup = Ember.lookup, lookup;
Model = Ember.Object.extend({
validate: function(property) {
this.errors.set(property, 'Error!');
},
}
});

module('input helpers', {
Expand Down

0 comments on commit 92b7ab7

Please sign in to comment.