Skip to content

Commit 8fecf13

Browse files
authored
use get for nested properties (#28)
1 parent 9034f02 commit 8fecf13

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

addon/components/bs-form/element.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import BsFormElement from 'ember-bootstrap/components/bs-form/element';
2-
import { action } from '@ember/object';
2+
import { action, get } from '@ember/object';
33
import { dependentKeyCompat } from '@ember/object/compat';
44

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

88
@dependentKeyCompat
99
get errors() {
10-
let error = this.model?.error?.[this.property]?.validation;
10+
let error = get(this, `model.error.${this.property}.validation`);
1111
return error ? [error] : [];
1212
}
1313

tests/integration/components/bs-form-element-test.js

+34
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ module('Integration | Component | bs form element', function(hooks) {
1717
]
1818
};
1919

20+
const nestedValidation = {
21+
nested: {
22+
name: [
23+
validatePresence(true),
24+
validateLength({ min: 4 })
25+
]
26+
}
27+
};
28+
2029
test('form is submitted if valid and validation success shown', async function(assert) {
2130
let model = {
2231
name: '1234',
@@ -66,6 +75,31 @@ module('Integration | Component | bs form element', function(hooks) {
6675
assert.verifySteps(['Invalid action has been called.']);
6776
});
6877

78+
test('validation nested errors are shown on submit', async function(assert) {
79+
let model = {
80+
nested: { name: '' }
81+
};
82+
83+
this.set('model', model);
84+
this.set('validation', nestedValidation);
85+
this.submitAction = function() {
86+
assert.ok(false, 'submit action must not been called.');
87+
};
88+
this.invalidAction = function() {
89+
assert.step('Invalid action has been called.');
90+
};
91+
92+
await render(hbs`
93+
<BsForm @model={{changeset this.model this.validation}} @onSubmit={{this.submitAction}} @onInvalid={{this.invalidAction}} as |form|>
94+
<form.element @label="Name" @property="nested.name" />
95+
</BsForm>
96+
`);
97+
98+
await triggerEvent('form', 'submit');
99+
assert.dom('input').hasClass('is-invalid', 'input has error class');
100+
assert.verifySteps(['Invalid action has been called.']);
101+
});
102+
69103
test('validation errors are shown after blur', async function(assert) {
70104
this.set('model', { name: '' });
71105
this.set('validation', validation);

0 commit comments

Comments
 (0)