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

FIO-7395: Fixed the issue with loading nested form #5520

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 34 additions & 2 deletions src/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,43 @@ export default class Form extends Element {
};
}

/**
* Check Subdirectories path and provide correct options
*
* @param {string} url - The the URL of the form json.
* @return {*}
*/
getFormInitOptions(url) {
const options = {};
const urlParts = Formio.getUrlParts(url);
if (!urlParts) {
return options;
}
const baseUrl = `${urlParts[1]}${urlParts[2]}`;
// Subdirectories path must be '/projectId/formId'
const path = urlParts[3]?.split('?')[0]?.split('/');
if (path?.length !== 3) {
roma-formio marked this conversation as resolved.
Show resolved Hide resolved
return options;
}
path.shift();
const [projectId, formId] = path;
// Detect Subdirectories path type when baseUrl wasn't set for this url
if (baseUrl !== Formio.baseUrl && projectId && formId) {
return {
base: baseUrl,
project: `${baseUrl}/${projectId}`,
};
}

return {};
}

setForm(formParam) {
let result;
formParam = formParam || this.form;
if (typeof formParam === 'string') {
const formio = new Formio(formParam);
const options = this.getFormInitOptions(formParam);
const formio = new Formio(formParam, options);
let error;
this.loading = true;
result = this.getSubmission(formio, this.options)
Expand All @@ -185,7 +217,7 @@ export default class Form extends Element {
}
this.loading = false;
this.instance = this.instance || this.create(form.display);
this.instance.url = formParam;
this.instance.setUrl(formParam, options);
this.instance.nosubmit = false;
this._form = this.instance.form = form;
if (submission) {
Expand Down
40 changes: 40 additions & 0 deletions src/Formio.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,46 @@ describe('Formio.js Tests', () => {
];
}
},
{
name: 'Should return correct options for form url with Subdirectories path',
test() {
let form = new Formio.Form();
let options = form.getFormInitOptions('http://localhost:3000/fakeproject/fakeform');
assert.deepEqual(options, {
base: 'http://localhost:3000',
project: 'http://localhost:3000/fakeproject',
});

form = new Formio.Form();
options = form.getFormInitOptions(`${Formio.baseUrl}/fakeproject/fakeform`);
assert.deepEqual(options, {});
}
},
{
name: 'Should set correct formio base and project url for form with Subdirectories path',
test() {
const formElement = document.createElement('div');
return Formio.createForm(formElement, 'http://localhost:3000/fakeproject/fakeform')
.then((form) => {
assert.equal(form.formio.base, 'http://localhost:3000');
assert.equal(form.formio.projectUrl, 'http://localhost:3000/fakeproject');
});
},
mock() {
return {
url: 'http://localhost:3000/fakeproject/fakeform',
method: 'GET',
response() {
return {
headers: {
'Content-Type': 'application/json',
},
body: {}
};
}
};
},
},
];

tests.forEach(testCapability);
Expand Down
8 changes: 7 additions & 1 deletion src/components/form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,13 @@ export default class FormComponent extends Component {
}
else if (this.formSrc) {
this.subFormLoading = true;
return (new Formio(this.formSrc)).loadForm({ params: { live: 1 } })
const options = this.root.formio?.base && this.root.formio?.projectUrl
? {
base: this.root.formio.base,
project: this.root.formio.projectUrl,
}
: {};
return (new Formio(this.formSrc, options)).loadForm({ params: { live: 1 } })
.then((formObj) => {
this.formObj = formObj;
if (this.options.pdf && this.component.useOriginalRevision) {
Expand Down
Loading