Configuration
Pass one options object to formsub.newForm(options).
Required
| Option | Type | Description |
|---|---|---|
formId | string | Form element id (not a selector). |
submitButton | string | CSS selector for the submit control. |
ajaxUrl | string | URL passed to fetch. |
If any of these are missing, or the form / button is not in the DOM, newForm logs an error and returns without binding.
Optional
| Option | Type | Default | Description |
|---|---|---|---|
method | string | 'POST' | HTTP method. No body on GET / HEAD. |
data | object | null | Extra key/value pairs appended to FormData. |
indicator | string | null | CSS selector for a loading element. |
recaptcha | boolean | string | null | Enable reCAPTCHA when truthy and not "false". |
recaptcha_site_key | string | null | Site key for grecaptcha.execute. |
iti | false | object | false | intl-tel-input setup. |
responseType | 'default' | 'swal' | 'default' | Inline errors, or SweetAlert2. |
callback | function | null | Replaces the default response handler. |
Full example
js
formsub.newForm({
formId: 'contact-form',
submitButton: '#submit-button',
ajaxUrl: '/api/contact',
method: 'POST',
data: { source: 'homepage', campaign: 'spring' },
indicator: '#loading',
recaptcha: true,
recaptcha_site_key: 'YOUR_SITE_KEY',
iti: { id: 'phone', country: 'BG' },
responseType: 'swal',
});Extra fields (data)
Static values appended to FormData after the visible form fields:
js
data: {
source: 'homepage',
userId: 42,
}Notes:
- Keys become field names on the server.
- Prefer strings or numbers. Nested objects are not flattened.
- If a key already exists on the form, both values are sent.
datais captured when you callnewForm. For values that change on every submit, append them in aformsub:BeforeSubmitlistener instead.
Loading indicator
html
<p id="loading" class="hidden">Sending…</p>js
indicator: '#loading'While the request is in flight, formsub:
- Adds
disabledon the submit button - Removes
hiddenanduk-hiddenon the indicator
Both are restored in a finally block, including cancelled submits and client validation failures that return early.