Frontend APIs
Overview
The storefront side of WebForms is one form controller, MageMe_WebForms/js/form-controller.js, plus a form
engine per rendered form, MageMe_WebForms/js/form.js. Both run on Luma, Hyvä and Breeze. The controller creates
each engine through a factory: MageMe_WebForms/js/submit on Luma and Breeze, Hyva_MageMeWebForms/js/hyva-submit.js
on Hyvä. The Hyvä factory creates the same form.js engine, with Hyvä's validation and form key.
Every rendered form has a uid: a short random id for this render, such as VIDN4O. The same form shown
twice on a page gets two uids. Element ids are built from it: the <form> is webform_<uid>, its wrapper is
webform_<uid>_form, the success message is webform_<uid>_success_text. The numeric form id is a different
value; the engine carries it as formId.
The controller boots itself. It mounts every form in the page, watches the page for forms added later and tears down forms that leave it. Your code only listens and hooks in.
document.addEventListener('webforms_after_submit_success', (event) => {
const { uid, data } = event.detail;
const engine = window.mmForms?.[uid];
console.log('Form sent', engine?.formId, data.success);
});
Globals
| Name | What it is | Notes |
|---|---|---|
MageMe.WebForms.ready | A promise that resolves once the form controller script has loaded. | Wait on it before you call mount, swap or fetchFragment. |
MageMe.WebForms.mount(root) | Mounts the forms and fields inside root (an element or document) that aren't mounted yet. | Returns a promise of [{uid, failures, error}], one entry per form. failures lists fields that failed while the form still submits; error is set when the form itself never became ready. |
MageMe.WebForms.unmount(uid) | Tears down the form with this uid. | Late callbacks of its start-up become no-ops. |
MageMe.WebForms.unmountWithin(root) | Tears down every form inside root, root included. | |
MageMe.WebForms.fetchFragment(url, init) | fetch for a form fragment. | Resolves with the HTML only for a successful, unredirected text/html answer; a login redirect or an error page rejects. |
MageMe.WebForms.swap(target, html, inserted) | Replaces the content of target with html and mounts what arrived. | target must already be in the document. Forms already inside target are torn down first. inserted(target) runs after the markup is in the page and before the mount. A newer swap of the same target supersedes one still mounting. |
MageMe.WebForms.onLoad | Registry of On Load scripts by form id. | Filled by the script the server sends; see Form scripts. |
window.mmForms | The form engines on the page, by uid. | See Form engine. |
window.mmForm | The form engine constructor. | The controller creates the engines; you don't need to call it. |
window.mmLogic | The conditional logic engine constructor. | The controller creates one per form with logic. |
Markup the controller reads:
| Attribute | On | What it holds |
|---|---|---|
data-mm-form | <script type="application/json"> | The form descriptor, keyed by uid: form id, stack, submit options, stylesheets, and the logic, multistep and onLoad flags. |
data-mm-logic-config | <script type="application/json"> | The conditional logic rules of the form with this uid. |
data-mm-field | A field's element | The field descriptor that names the script the field needs. |
data-mm-mount="open" | Any container | Holds the forms and fields inside it back from automatic mounting. Remove the attribute and call mount when the container opens. |
These islands are data, not code: treat their shape as internal and read the values through the engine instead.
Form engine
window.mmForms[uid] is the engine of one rendered form. It exists once the event mm-form-ready<uid> has
fired.
| Member | What it does |
|---|---|
uid | The render uid. |
formId | The numeric form id, as a string. |
controls.form | The <form> element. |
controls.block | The wrapper webform_<uid>_form. |
controls.successText | The success message element. |
submit() | Validates and sends the form, the same as the visitor's click. Returns a promise. |
validate() | Runs the form's validation. Resolves to true or false. |
beforeSubmit | A hook you set: (engine) => …. It runs after webforms_before_submit and before validation. Return false to take the sending over: nothing is validated or sent and the form unlocks. |
afterSubmit | A hook you set: (engine, valid) => …. It runs after validation and, when the form was valid, after the send. |
destroy() | Tears down the engine only. To remove a form completely, with its field scripts and flags, call MageMe.WebForms.unmount(uid) instead. |
Events
<uid> is appended to the event name with no separator, for example mm_webform_loaded + VIDN4O.
| Event | Dispatched on | detail | When |
|---|---|---|---|
mm-form-ready<uid> | document | {uid} | The engine is in window.mmForms. |
mm_webform_fields_ready<uid> | window | {uid, form, failures} | Every field script has settled. |
mm_webform_loaded<uid> | window | {uid, form} | The form is fully ready: engine, logic, steps and the On Load script. The submit button is enabled at this point. |
webforms_before_submit | document | {form}, the engine | The visitor or your code started a send. |
webforms_after_submit_success | document | {uid, data}, the server's answer | The server accepted the submission. It fires before the After Submission Script and before any redirect. |
mm-form-reset | The <form>, bubbling | none | The form stays on screen after a successful send and has just been cleared. |
mm-form-data-loaded | document | {uid, formData} | Answers saved in the visitor's browser were restored into the form. |
The two window events also leave a flag behind: window['mm_webform_fields_ready' + uid] and
window['mm_webform_loaded' + uid] are true once they've fired, until the form is torn down. Check the flag
first if your code may run late.
Form scripts
Each form has two script settings in the Scripts section of its settings page: On Load Script and
After Submission Script. Both take plain JavaScript; no <script> tag. See
Form settings.
| Setting | How it runs | What your code gets |
|---|---|---|
| On Load Script | The server sends it as an external script from webforms/form/script/form_id/<id>/, wrapped as MageMe.WebForms.onLoad["<id>"] = function (ctx) { … }. It loads once per form id and runs once for each rendered copy of the form, after the engine, logic and steps are ready. The function is called synchronously: a returned promise is ignored and top-level await isn't allowed. | ctx.uid, ctx.formId, ctx.form (the <form> element), ctx.root (the same element), ctx.instance (the controller's record of this form). The engine is window.mmForms[ctx.uid]. |
| After Submission Script | The server returns it in the answer to a successful send, and the engine runs it inside its success handler, as a script of its own (see below). It runs after webforms_after_submit_success and before any redirect. | this is the engine, data is the server's answer. |
- The On Load script is part of the form's start-up. If it throws, or its address answers 404, the form never becomes ready: the error goes to the browser console, the form shows its general error message at the top and the submit button stays disabled. Test the script before the form goes live.
- The address serves the script only for an active form the visitor may open.
- An error thrown in the After Submission Script is caught and written to the browser console; the rest of the success handling goes on.
- The After Submission Script doesn't use
eval. After each submission the store keeps the rendered script for five minutes at its own address and hands it out once; the form loads it from there as an external script, so a policy that allows'self'inscript-src(Magento's does) runs it, also on a Hyvä page served from the Full Page Cache. Only when that address can't be loaded, for example on several servers without a shared cache, the text from the answer runs as a script element with the page's nonce; on a Hyvä page from the Full Page Cache there is no nonce, so a strict policy blocks that fallback. On a form, a redirect after the submission waits for the script. In the product options popup the script runs after the dialog closes, and doesn't run when the form redirects. The On Load script is a same-origin external file as well. - WebForms itself puts no inline event handlers (
onclickand the like) and nojavascript:links into the admin or the storefront. The admin's own scripts are files or are printed through Magento's secure renderer, which lets Magento's Content Security Policy allow them, so the admin works under a strict policy without'unsafe-inline'. The signature image on a customer's submission page loads without an inline script. - A script in an HTML element of a form opened in a popup or a slide-out that loads on first open is loaded
from the store's own address, once per page, before the form starts (after 5 seconds the form starts without it).
Its attributes are copied except
on…,nonceandsrc; a script that failed to load is tried again at the next opening. An import map or speculation rules work only when the form is on the page itself; in a popup they are left out. A module script that imports files by a relative path (./or../) or readsimport.metasees the store's script address instead of the page's, so use full paths there. The code is kept in the store's cache for five minutes and anyone who opens the form can read it. - Known limit with a Slick carousel: on a page without the slide-out and without a form above it, a Page Builder product carousel can carry the page's first copy of the WebForms start-up scripts in a slide, for example with a product request button. The carousel then tries to run those two scripts again for that slide and each of its copies: two synchronous requests to files the browser already has, and, under a strict Content Security Policy, two entries in the policy reports, because the repeat is blocked. Forms and buttons work as usual.
- Both scripts pass through the Magento template filter on the server. Keep
{{out of your code, or it may be read as a directive.
On Load Script that fills a Text field with the code campaign from the page address:
const params = new URLSearchParams(window.location.search);
const input = ctx.form.querySelector('[data-wf-field="campaign"] [data-wf-part~="field-input"]');
if (input && params.has('utm_campaign')) {
input.value = params.get('utm_campaign');
}
What's next
- Styling contract: the
data-wf-*attributes the selectors above rely on. - Theme and template overrides: why a template override must not print its
own
<script>, and where theme code goes instead. - Form settings: where a store owner pastes the two form scripts.