Skip to content

Instantly share code, notes, and snippets.

@alexBaizeau
Last active July 24, 2017 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexBaizeau/d61e7533518d589e87e35ff5c01d9fd6 to your computer and use it in GitHub Desktop.
Save alexBaizeau/d61e7533518d589e87e35ff5c01d9fd6 to your computer and use it in GitHub Desktop.
validator ember-data
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
validate(){
return this.get('model').validate();
}
}
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
import { validator, buildValidations } from 'ember-cp-validations';
const Validations = buildValidations({
system: validator('belongs-to'),
lines: validator('has-many')
});
export default Model.extend(
Validations, {
system: belongsTo('system'),
lines: hasMany('line')
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
import { validator, buildValidations } from 'ember-cp-validations';
const Validations = buildValidations({
name: validator('presence', true)
});
export default Model.extend(
Validations, {
name: attr('string')
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
import { validator, buildValidations } from 'ember-cp-validations';
const Validations = buildValidations({
name: validator('length', {
max: 10
})
});
export default Model.extend(
Validations, {
name: attr('string')
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
model() {
let line = this.store.createRecord('line'),
system = this.store.createRecord('system'),
invoice = this.store.createRecord('invoice');
invoice.set('system', system);
line.set('name', 'An invoice line');
invoice.get('system').set('name', 'foooo');
invoice.get('lines').pushObject(line);
return invoice;
}
});
System Name: <input value = {{model.system.name}}/>
<br>
=======================================================
<div>
Lines:
{{#each model.lines as |line|}}
<div>
name: <input value={{line.name}}/>
</div>
{{/each}}
</div>
<button {{action 'validate'}}>Validate</button>
<table>
<tr>
<th> Property </th>
<th> Value </th>
</tr>
<tr>
<td><pre>model.lines.firstObject.validations.isValid</pre></td>
<td>{{model.lines.firstObject.validations.isValid}}</td>
</tr>
<tr>
<td><pre>model.system.validations.isValid</pre></td>
<td>{{model.system.validations.isValid}}</td>
</tr>
<tr>
<td><pre>model.validations.isValid</pre></td>
<td>{{model.validations.isValid}}</td>
</tr>
</table>
{{outlet}}
<br>
<br>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1",
"ember-cp-validations": "3.3.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment