Skip to content

Commit

Permalink
Add support for Gravity Forms
Browse files Browse the repository at this point in the history
  • Loading branch information
dsagal committed Oct 3, 2023
1 parent 3ffd2a1 commit c48e33b
Showing 1 changed file with 55 additions and 11 deletions.
66 changes: 55 additions & 11 deletions grist-form-submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,23 @@ async function handleSubmitPlainForm(ev) {
window.location.href = successUrl;

} catch (err) {
console.warn("grist-form-submit error:", err.message);
// Find an element to use for the validation message to alert the user.
let scapegoat = null;
(
(scapegoat = ev.submitter)?.setCustomValidity ||
(scapegoat = ev.target.querySelector('input[type=submit]'))?.setCustomValidity ||
(scapegoat = ev.target.querySelector('button'))?.setCustomValidity ||
(scapegoat = [...ev.target.querySelectorAll('input')].pop())?.setCustomValidity
)
scapegoat?.setCustomValidity("Form misconfigured: " + err.message);
ev.target.reportValidity();
reportSubmitError(ev, err);
}
}

function reportSubmitError(ev, err) {
console.warn("grist-form-submit error:", err.message);
// Find an element to use for the validation message to alert the user.
let scapegoat = null;
(
(scapegoat = ev.submitter)?.setCustomValidity ||
(scapegoat = ev.target.querySelector('input[type=submit]'))?.setCustomValidity ||
(scapegoat = ev.target.querySelector('button'))?.setCustomValidity ||
(scapegoat = [...ev.target.querySelectorAll('input')].pop())?.setCustomValidity
)
scapegoat?.setCustomValidity("Form misconfigured: " + err.message);
ev.target.reportValidity();
}

// Handle submissions for Contact Form 7 forms.
async function handleSubmitWPCF7(ev) {
Expand All @@ -115,6 +118,47 @@ async function handleSubmitWPCF7(ev) {
}
}

function setUpGravityForms(options) {
// Use capture to get the event before GravityForms processes it.
document.addEventListener('submit', ev => handleSubmitGravityForm(ev, options), true);
}

async function handleSubmitGravityForm(ev, options) {
try {
ev.preventDefault();
ev.stopPropagation();

const docUrl = options.docUrl;
const tableId = options.tableId;
if (!docUrl) { throw new Error("setUpGravityForm: missing docUrl option"); }
if (!tableId) { throw new Error("setUpGravityForm: missing tableId option"); }

const f = new FormData(ev.target);
for (const key of Array.from(f.keys())) {
// Skip fields other than input fields.
if (!key.startsWith("input_")) {
f.delete(key);
continue;
}
// Rename multiple fields to use "[]" convention rather than ".N" convention.
const multi = key.split(".");
if (multi.length > 1) {
f.append(multi[0] + "[]", f.get(key));
f.delete(key);
}
}
console.warn("Processed FormData", f);
await gristFormSubmit(docUrl, tableId, f);

// Follow through by doing the form submission normally.
ev.target.submit();

} catch (err) {
reportSubmitError(ev, err);
return;
}
}

window.gristFormSubmit = gristFormSubmit;
document.addEventListener('submit', handleSubmitPlainForm);
document.addEventListener('wpcf7mailsent', handleSubmitWPCF7);
Expand Down

0 comments on commit c48e33b

Please sign in to comment.