1
1
import BsFormElement from 'ember-bootstrap/components/bs-form/element' ;
2
2
import { action , get } from '@ember/object' ;
3
- import { dependentKeyCompat } from '@ember/object/compat' ;
4
3
import { isNone , typeOf } from '@ember/utils' ;
5
4
6
5
export default class BsFormElementWithChangesetValidationsSupport extends BsFormElement {
7
6
'__ember-bootstrap_subclass' = true ;
8
7
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
21
8
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` ) ;
23
13
24
14
// no messages
25
15
if ( isNone ( errors ) ) {
@@ -36,7 +26,7 @@ export default class BsFormElementWithChangesetValidationsSupport extends BsForm
36
26
}
37
27
38
28
get hasValidator ( ) {
39
- return typeof this . model ?. validate === 'function' ;
29
+ return typeof this . args . model ?. validate === 'function' ;
40
30
}
41
31
42
32
// Ember Changeset does not validate the initial state. Properties are not
@@ -71,10 +61,10 @@ export default class BsFormElementWithChangesetValidationsSupport extends BsForm
71
61
72
62
// run initial validation if
73
63
// - visibility of validations changed
74
- let canValidate = this . hasValidator && this . property ;
64
+ let canValidate = this . hasValidator && this . args . property ;
75
65
let validationVisibilityChanged = ! validationShowBefore && this . showOwnValidation ;
76
66
if ( canValidate && validationVisibilityChanged ) {
77
- await this . model . validate ( this . property ) ;
67
+ await this . args . model . validate ( this . args . property ) ;
78
68
}
79
69
}
80
70
}
0 commit comments