Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix validation for nested properties #28

Merged
merged 2 commits into from
Aug 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions addon/components/bs-form/element.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import BsFormElement from 'ember-bootstrap/components/bs-form/element';
import { action } from '@ember/object';
import { action, get } from '@ember/object';
import { dependentKeyCompat } from '@ember/object/compat';

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

@dependentKeyCompat
get errors() {
let error = this.model?.error?.[this.property]?.validation;
let error = get(this, `model.error.${this.property}.validation`);
return error ? [error] : [];
}

Expand Down
34 changes: 34 additions & 0 deletions tests/integration/components/bs-form-element-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ module('Integration | Component | bs form element', function(hooks) {
]
};

const nestedValidation = {
nested: {
name: [
validatePresence(true),
validateLength({ min: 4 })
]
}
};

test('form is submitted if valid and validation success shown', async function(assert) {
let model = {
name: '1234',
Expand Down Expand Up @@ -66,6 +75,31 @@ module('Integration | Component | bs form element', function(hooks) {
assert.verifySteps(['Invalid action has been called.']);
});

test('validation nested errors are shown on submit', async function(assert) {
let model = {
nested: { name: '' }
};

this.set('model', model);
this.set('validation', nestedValidation);
this.submitAction = function() {
assert.ok(false, 'submit action must not been called.');
};
this.invalidAction = function() {
assert.step('Invalid action has been called.');
};

await render(hbs`
<BsForm @model={{changeset this.model this.validation}} @onSubmit={{this.submitAction}} @onInvalid={{this.invalidAction}} as |form|>
<form.element @label="Name" @property="nested.name" />
</BsForm>
`);

await triggerEvent('form', 'submit');
assert.dom('input').hasClass('is-invalid', 'input has error class');
assert.verifySteps(['Invalid action has been called.']);
});

test('validation errors are shown after blur', async function(assert) {
this.set('model', { name: '' });
this.set('validation', validation);
Expand Down