Skip to content

Instantly share code, notes, and snippets.

@Subtletree
Last active August 10, 2018 01:17
Show Gist options
  • Save Subtletree/a863a3cbd1e43164669f3aebb5cfdad9 to your computer and use it in GitHub Desktop.
Save Subtletree/a863a3cbd1e43164669f3aebb5cfdad9 to your computer and use it in GitHub Desktop.
cp validations parentAttribute
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
});
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({
posts: validator('has-many'),
name: validator('presence', true)
});
export default Model.extend(Validations, {
posts: hasMany('post'),
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({
title: validator('presence', true)
});
export default Model.extend(Validations, {
author: belongsTo('author'),
title: attr('string')
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
let author = this.store.createRecord('author', { name:'Sam' });
let post = this.store.createRecord('post', { author });
// Can see parentAttribute is a string in console
console.log('Error: ', author.validations.errors[0]);
return author;
}
});
{{#each model.validations.errors as |error|}}
<p><b>parentAttribute</b>: {{error.parentAttribute}}</p>
<p><b>attribute</b>: {{error.attribute}}</p>
<p><b>message</b>: {{error.message}}</p>
{{/each}}
<i>Error object is also logged to console</i>
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0",
"ember-cp-validations": "3.5.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment