Skip to content

Instantly share code, notes, and snippets.

@jelhan
Last active March 12, 2020 03:03
Show Gist options
  • Save jelhan/01db3b47199b70d64fdbff3013cc696b to your computer and use it in GitHub Desktop.
Save jelhan/01db3b47199b70d64fdbff3013cc696b to your computer and use it in GitHub Desktop.
ember-bootstrap-changeset-validations-upgrade-v3-bug
import Ember from 'ember';
import Changeset from 'ember-changeset';
import lookupValidator from 'ember-changeset-validations';
import { validateLength } from 'ember-changeset-validations/validators';
const Validations = {
name: validateLength({ min: 2 }),
};
export default Ember.Component.extend({
init() {
this._super(...arguments);
this.model = new Changeset({ name: 'f' }, lookupValidator(Validations), Validations);
this.property = 'name';
},
setupValidations() {
let key = `model.error.${this.get('property')}.validation`;
defineProperty(this, 'errors', computed(`${key}[]`, function() {
return A(this.get(key));
}));
},
validate() {
let m = this.model;
assert('Model must be a Changeset instance', m && typeof m.validate === 'function');
return new RSVP.Promise( function(resolve, reject) {
m.validate().then(
() => {
model.get('isValid') ? resolve() : reject();
},
reject
);
});
},
actions: {
validate() {
this.validate();
}
},
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
<MyComponent />
Validation: {{this.model.error.name.validation}}<br>
<button {{on "click" (action "validate")}}>validate</button>
{
"version": "0.16.1",
"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.14.3",
"ember-template-compiler": "3.14.3",
"ember-testing": "3.14.3"
},
"addons": {
"ember-changeset": "3.0.6",
"ember-changeset-validations": "3.0.2",
"ember-data": "3.14.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment