11import BsFormElement from 'ember-bootstrap/components/bs-form/element' ;
22import { action , get } from '@ember/object' ;
3- import { dependentKeyCompat } from '@ember/object/compat' ;
43import { isNone , typeOf } from '@ember/utils' ;
54
65export default class BsFormElementWithChangesetValidationsSupport extends BsFormElement {
76 '__ember-bootstrap_subclass' = true ;
87
9- // We convert
10- //
11- // `model.error.${this.property}.validation` which could be either a string or an array
12- // see https://github.com/validated-changeset/validated-changeset/#error
13- //
14- // into
15- //
16- // Ember Bootstrap expects errors property of FormElement to be an array of validation messages:
17- // see https://www.ember-bootstrap.com/api/classes/Components.FormElement.html#property_errors
18- //
19- // If the if the property is valid but no validation is present `model.error.[this.property] could also be undefined.
20- @dependentKeyCompat
218 get errors ( ) {
22- let errors = get ( this , `model.error.${ this . property } .validation` ) ;
9+ let { model, property } = this . args ;
10+
11+ // must use `get` method to support nested properties
12+ let errors = get ( model , `error.${ property } .validation` ) ;
2313
2414 // no messages
2515 if ( isNone ( errors ) ) {
@@ -36,7 +26,7 @@ export default class BsFormElementWithChangesetValidationsSupport extends BsForm
3626 }
3727
3828 get hasValidator ( ) {
39- return typeof this . model ?. validate === 'function' ;
29+ return typeof this . args . model ?. validate === 'function' ;
4030 }
4131
4232 // Ember Changeset does not validate the initial state. Properties are not
@@ -71,10 +61,10 @@ export default class BsFormElementWithChangesetValidationsSupport extends BsForm
7161
7262 // run initial validation if
7363 // - visibility of validations changed
74- let canValidate = this . hasValidator && this . property ;
64+ let canValidate = this . hasValidator && this . args . property ;
7565 let validationVisibilityChanged = ! validationShowBefore && this . showOwnValidation ;
7666 if ( canValidate && validationVisibilityChanged ) {
77- await this . model . validate ( this . property ) ;
67+ await this . args . model . validate ( this . args . property ) ;
7868 }
7969 }
8070}
0 commit comments