Skip to content

Commit

Permalink
support Ember Bootstrap after <BsForm::element> is refactored to @gli…
Browse files Browse the repository at this point in the history
…mmer/component (#34)
  • Loading branch information
jelhan committed Dec 4, 2020
1 parent daaa0e8 commit ecf2c4f
Show file tree
Hide file tree
Showing 4 changed files with 1,915 additions and 190 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ further details.
Compatibility
------------------------------------------------------------------------------

* Ember Bootstrap v3
* Ember Bootstrap v4.5 or above
* Ember Changeset and Ember Changeset Validations v3
* Ember.js v3.16 or above
* Ember CLI v2.13 or above
Expand Down
24 changes: 7 additions & 17 deletions addon/components/bs-form/element.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import BsFormElement from 'ember-bootstrap/components/bs-form/element';
import { action, get } from '@ember/object';
import { dependentKeyCompat } from '@ember/object/compat';
import { isNone, typeOf } from '@ember/utils';

export default class BsFormElementWithChangesetValidationsSupport extends BsFormElement {
'__ember-bootstrap_subclass' = true;

// We convert
//
// `model.error.${this.property}.validation` which could be either a string or an array
// see https://github.com/validated-changeset/validated-changeset/#error
//
// into
//
// Ember Bootstrap expects errors property of FormElement to be an array of validation messages:
// see https://www.ember-bootstrap.com/api/classes/Components.FormElement.html#property_errors
//
// If the if the property is valid but no validation is present `model.error.[this.property] could also be undefined.
@dependentKeyCompat
get errors() {
let errors = get(this, `model.error.${this.property}.validation`);
let { model, property } = this.args;

// must use `get` method to support nested properties
let errors = get(model, `error.${property}.validation`);

// no messages
if (isNone(errors)) {
Expand All @@ -36,7 +26,7 @@ export default class BsFormElementWithChangesetValidationsSupport extends BsForm
}

get hasValidator() {
return typeof this.model?.validate === 'function';
return typeof this.args.model?.validate === 'function';
}

// Ember Changeset does not validate the initial state. Properties are not
Expand Down Expand Up @@ -71,10 +61,10 @@ export default class BsFormElementWithChangesetValidationsSupport extends BsForm

// run initial validation if
// - visibility of validations changed
let canValidate = this.hasValidator && this.property;
let canValidate = this.hasValidator && this.args.property;
let validationVisibilityChanged = !validationShowBefore && this.showOwnValidation;
if (canValidate && validationVisibilityChanged) {
await this.model.validate(this.property);
await this.args.model.validate(this.args.property);
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"bootstrap": "^4.3.1",
"broccoli-asset-rev": "^3.0.0",
"ember-auto-import": "^1.5.3",
"ember-bootstrap": "^3.1.1",
"ember-bootstrap": "^4.5.0",
"ember-cli": "~3.16.0",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-eslint": "^5.1.0",
Expand Down

0 comments on commit ecf2c4f

Please sign in to comment.