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

[Forms, ImportFromJSONAction plugin] display form error inside form #7986

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions e2e/helper/addInitInvalidFileInputObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class DomainObjectViewProvider {
constructor(openmct) {
this.key = 'doViewProvider';
this.name = 'Domain Object View Provider';
this.openmct = openmct;
}

canView(domainObject) {
return domainObject.type === 'imageFileInput' || domainObject.type === 'jsonFileInput';
}

view(domainObject, objectPath) {
let content;

return {
show: function (element) {
const body = domainObject.selectFile.body;
const type = typeof body;

content = document.createElement('div');
content.id = 'file-input-type';
content.textContent = JSON.stringify(type);
element.appendChild(content);
},
destroy: function (element) {
element.removeChild(content);
content = undefined;
}
};
}
}

document.addEventListener('DOMContentLoaded', () => {
const openmct = window.openmct;

openmct.types.addType('jsonFileInput', {
key: 'jsonFileInput',
name: 'JSON File Input Object',
creatable: true,
form: [
{
name: 'Upload File',
key: 'selectFile',
control: 'file-input',
required: true,
text: 'Select File...',
type: 'application/json',
validate: function (data, fail) {
return fail('Test error json');
},
property: ['selectFile']
}
]
});

openmct.objectViews.addProvider(new DomainObjectViewProvider(openmct));
});
27 changes: 27 additions & 0 deletions e2e/tests/functional/forms.e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ test.describe('Form File Input Behavior', () => {
});
});

test.describe('Form File Invalid Input Behavior', () => {
test.beforeEach(async ({ page }) => {
await page.addInitScript({
path: fileURLToPath(new URL('../../helper/addInitInvalidFileInputObject.js', import.meta.url))
});
});

test('An invalid file will display an error', async ({ page }) => {
await page.goto('./', { waitUntil: 'domcontentloaded' });

await page.getByRole('button', { name: 'Create' }).click();
await page.getByRole('menuitem', { name: 'JSON File Input Object' }).click();

await page.setInputFiles('#fileElem', jsonFilePath);

await expect(page.getByLabel('Save')).toBeDisabled();

await expect(
page
.locator('.form-error', {
hasText: 'Test error json'
})
.first()
).toBeVisible();
});
});

test.describe('Persistence operations @addInit', () => {
// add non persistable root item
test.beforeEach(async ({ page }) => {
Expand Down
12 changes: 10 additions & 2 deletions src/api/forms/components/FormRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
{{ row.name }}
</label>
<div class="c-form-row__state-indicator" :class="reqClass"></div>
<div v-if="row.control" ref="rowElement" class="c-form-row__controls"></div>
<div class="c-form-row__controls">
<div v-if="row.control" ref="rowElement"></div>
<div v-if="errorMessage" class="form-error">{{ errorMessage }}</div>
</div>
</div>
</template>

Expand Down Expand Up @@ -54,6 +57,7 @@
emits: ['on-change'],
data() {
return {
errorMessage: null,
formControl: this.openmct.forms.getFormControl(this.row.control),
valid: undefined,
visited: false
Expand Down Expand Up @@ -105,6 +109,7 @@
this.$emit('on-change', data);
},
validateRow(data) {
this.errorMessage = null;
let valid = true;
if (this.row.required) {
valid = data.value !== undefined && data.value !== null && data.value !== '';
Expand All @@ -122,7 +127,10 @@

const validate = data.model.validate;
if (valid && validate) {
valid = validate(data);
valid = validate(data, (errorMessage = null) => {
this.errorMessage = errorMessage;
return false;

Check warning on line 132 in src/api/forms/components/FormRow.vue

View check run for this annotation

Codecov / codecov/patch

src/api/forms/components/FormRow.vue#L131-L132

Added lines #L131 - L132 were not covered by tests
});
}

return Boolean(valid);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/importFromJSONAction/ImportFromJSONAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@
* @param {Object} data
* @returns {boolean}
*/
_validateJSON(data) {
_validateJSON(data, fail) {
const value = data.value;
const objectTree = value && value.body;
let json;
Expand All @@ -399,7 +399,7 @@
}

if (!success) {
this.openmct.notifications.error(
fail(

Check warning on line 402 in src/plugins/importFromJSONAction/ImportFromJSONAction.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/importFromJSONAction/ImportFromJSONAction.js#L402

Added line #L402 was not covered by tests
'Invalid File: The selected file was either invalid JSON or was not formatted properly for import into Open MCT.'
);
}
Expand Down
Loading