Skip to content

Instantly share code, notes, and snippets.

@validkeys
Created July 29, 2014 12:40
Show Gist options
  • Save validkeys/27093f0c436423b5f219 to your computer and use it in GitHub Desktop.
Save validkeys/27093f0c436423b5f219 to your computer and use it in GitHub Desktop.
Validated Ember Input
{{v-input classNames="spec-fn" value=first_name name="first_name" model=model}}
import Ember from 'ember';
export default Ember.Component.extend({
init: function(){
this._super();
this.set('errors',[]);
},
model: {},
name: "",
errors: [],
updateErrors: function() {
if (this.get('model.isValid')){
return this.set('errors',[]);
} else {
var msgs = this.get('model.errors.' + this.get('name'));
return this.set('errors', msgs);
}
},
didInsertElement: function(){
this.addObserver("model.errors." + this.get('name'), this.updateErrors);
}
});
{{input type="text" name=name value=value classNames=classNames}}
{{#if errors}}
<ul>
{{#each error in errors}}
<li>{{error}}</li>
{{/each}}
</ul>
{{/if}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment