Skip to content

Commit 21636fd

Browse files
committed
do not break forms which are not using a changeset
1 parent 7f0a5ec commit 21636fd

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

addon/components/bs-form/element.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export default class BsFormElementWithChangesetValidationsSupport extends BsForm
77

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

1414
get hasValidator() {
15-
return typeof this.model.validate === 'function';
15+
return typeof this.model?.validate === 'function';
1616
}
1717

1818
// Ember Changeset does not validate the initial state. Properties are not

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,54 @@ module('Integration | Component | bs form element', function(hooks) {
133133
await blur('input');
134134
assert.dom('input').hasClass('is-valid', 'validation error is shown after focus out');
135135
});
136+
137+
test('does not break forms which are not using a changeset as model', async function(assert) {
138+
this.set('model', { name: '' });
139+
this.set('submitAction', () => {
140+
assert.step('submit action has been called');
141+
});
142+
143+
await render(hbs`
144+
<BsForm @model={{this.model}} @onSubmit={{this.submitAction}} as |form|>
145+
<form.element @label="Name" @property="name" />
146+
</BsForm>
147+
`);
148+
assert.dom('input').doesNotHaveClass('is-valid');
149+
assert.dom('input').doesNotHaveClass('is-invalid');
150+
151+
await fillIn('input', 'Rosa');
152+
await blur('input');
153+
assert.dom('input').doesNotHaveClass('is-valid');
154+
assert.dom('input').doesNotHaveClass('is-invalid');
155+
156+
await triggerEvent('form', 'submit');
157+
assert.dom('input').doesNotHaveClass('is-valid');
158+
assert.dom('input').doesNotHaveClass('is-invalid');
159+
assert.verifySteps(['submit action has been called']);
160+
});
161+
162+
test('does not break for forms which are not having a model at all', async function(assert) {
163+
this.set('submitAction', () => {
164+
assert.step('submit action has been called');
165+
});
166+
this.set('noop', () => {});
167+
168+
await render(hbs`
169+
<BsForm @onSubmit={{this.submitAction}} as |form|>
170+
<form.element @label="Name" @property="name" @onChange={{this.noop}} />
171+
</BsForm>
172+
`);
173+
assert.dom('input').doesNotHaveClass('is-valid');
174+
assert.dom('input').doesNotHaveClass('is-invalid');
175+
176+
await fillIn('input', 'Rosa');
177+
await blur('input');
178+
assert.dom('input').doesNotHaveClass('is-valid');
179+
assert.dom('input').doesNotHaveClass('is-invalid');
180+
181+
await triggerEvent('form', 'submit');
182+
assert.dom('input').doesNotHaveClass('is-valid');
183+
assert.dom('input').doesNotHaveClass('is-invalid');
184+
assert.verifySteps(['submit action has been called']);
185+
});
136186
});

0 commit comments

Comments
 (0)