FetchIt is a modern, lightweight component for MODX Revolution designed for submitting and processing forms using the native Fetch API. It replaces legacy solutions (like AjaxForm), enabling forms to work without page reloads and without relying on heavy libraries like jQuery.
- Why choose FetchIt?
- Installation
- Quick Start
- 1. Creating a form chunk
- 2. Calling the Snippet
- Snippet Parameters
- JavaScript API and Events
- Main Events
- Example 1: Adding data before submitting
- ΠΡΠΈΠΌΠ΅Ρ 2: ΠΡΠΏΠ»ΡΠ²Π°ΡΡΠΈΠ΅ ΡΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΡ (Notyf)
- Processing with a Custom Snippet
- Working with files
- Migrating from AjaxForm
- Example: Integration with SweetAlert2
- Conclusion
Why choose FetchIt?
- No dependencies. The component runs on pure JavaScript. You don’t need to include jQuery, which speeds up site loading and improves PageSpeed.
- Lightweight. The script weighs only ~5 KB and is included with the
deferattribute, without blocking page rendering. - Flexibility. Easily integrates with any CSS frameworks (Bootstrap, Tailwind) and JS notification libraries (Notyf, SweetAlert2).
- Easy migration. The functionality is very similar to AjaxForm + FormIt, making the transition intuitive.
Installation
FetchIt is available for free installation via the “Package Installer” in the MODX admin panel. Main sources:
- Official MODX repository.
- Add-on store modstore.pro.
- GitHub repository (for manual building).
Quick Start
Running the form requires just two steps: preparing the HTML markup (chunk) and calling the snippet on the page.
1. Creating a form chunk
Create a chunk (for example, tpl.MyForm) with the following markup. Please note the following important attributes:
- Fields (input, textarea) must have a
nameattribute. - The submit button must have
type="submit". - To display validation errors, use spans with the
data-error="field_name"attribute.
<form action="[[~[[*id]]]]" method="post">
<div class="form-group">
<label for="name">ΠΠΌΡ:</label>
<input type="text" name="name" id="name" value="[[+fi.name]]" class="form-control" required>
<!-- Π‘ΡΠ΄Π° FetchIt Π²ΡΡΠ°Π²ΠΈΡ ΡΠ΅ΠΊΡΡ ΠΎΡΠΈΠ±ΠΊΠΈ ΠΏΡΠΈ Π²Π°Π»ΠΈΠ΄Π°ΡΠΈΠΈ -->
<span class="error" data-error="name">[[+fi.error.name]]</span>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" name="email" id="email" value="[[+fi.email]]" class="form-control" required>
<span class="error" data-error="email">[[+fi.error.email]]</span>
</div>
<div class="form-group">
<label for="message">Π‘ΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅:</label>
<textarea name="message" id="message" class="form-control" required>[[+fi.message]]</textarea>
<span class="error" data-error="message">[[+fi.error.message]]</span>
</div>
<button type="submit" class="btn btn-primary">ΠΡΠΏΡΠ°Π²ΠΈΡΡ</button>
<!-- Π‘ΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ ΠΎΠ± ΡΡΠΏΠ΅Ρ
Π΅/ΠΎΡΠΈΠ±ΠΊΠ΅ (ΠΎΠΏΡΠΈΠΎΠ½Π°Π»ΡΠ½ΠΎ) -->
[[+fi.success:is=`1`:then=`<div class="alert alert-success">[[+fi.successMessage]]</div>`]]
[[+fi.validation_error:is=`1`:then=`<div class="alert alert-danger">[[+fi.validationErrorMessage]]</div>`]]
</form>
2. Calling the Snippet
Place a call to FetchIt on the page where the form should be. Inside FetchIt, we call the standard FormIt for email processing and validation.
Important! The snippet must be called uncached (with the exclamation point
[[!FetchIt]]).
[[!FetchIt?
&snippet=`FormIt`
&form=`tpl.MyForm`
&hooks=`email`
&emailSubject=`ΠΠΎΠ²ΠΎΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Ρ ΡΠ°ΠΉΡΠ°`
&emailTo=`info@mysite.com`
&validate=`name:required,email:required:email,message:required`
&validationErrorMessage=`ΠΠΎΠΆΠ°Π»ΡΠΉΡΡΠ°, ΠΈΡΠΏΡΠ°Π²ΡΡΠ΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π² ΡΠΎΡΠΌΠ΅!`
&successMessage=`Π‘ΠΏΠ°ΡΠΈΠ±ΠΎ! ΠΠ°ΡΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΡΡΠΏΠ΅ΡΠ½ΠΎ ΠΎΡΠΏΡΠ°Π²Π»Π΅Π½ΠΎ.`
]]
Snippet Parameters
FetchIt accepts its own parameters to customize frontend behavior, and passes all unknown parameters to the snippet handler (FormIt by default).
| Parameter | Default | Description |
|---|---|---|
&form |
tpl.FetchIt.example | Name of the chunk with the form’s HTML code. |
&snippet |
FormIt | Name of the snippet that will process data on the server (validation, hooks). |
&actionUrl |
[[+assetsUrl]]action.php | Connector URL for sending requests. Typically no changes are required. |
&clearFieldsOnSuccess |
1 | Clear form fields after successful submission (1 – Yes, 0 – No). |
&hooks, &validate, etc. |
β | Any FormIt parameters (or your custom snippet). |
JavaScript API and Events
FetchIt provides a powerful API for interacting with a form on the client. You can intercept the submission process, modify data, or display custom notifications.
Main Events
fetchit:beforeβ Fires before submitting. A great place for additional JavaScript validation or adding data to FormData.fetchit:successβ Fires upon a successful response from the server (success: true).fetchit:errorβ Fires upon an error (validation failed or a server error).
Example 1: Adding data before submitting
document.addEventListener('fetchit:before', (e) => {
const { form, formData } = e.detail;
// ΠΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΠΎΠ΅ ΠΏΠΎΠ»Π΅, ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ Π½Π΅Ρ Π² Π²Π΅ΡΡΡΠΊΠ΅
formData.append('page_title', document.title);
console.log('Π€ΠΎΡΠΌΠ° ΠΎΡΠΏΡΠ°Π²Π»ΡΠ΅ΡΡΡ...');
});
ΠΡΠΈΠΌΠ΅Ρ 2: ΠΡΠΏΠ»ΡΠ²Π°ΡΡΠΈΠ΅ ΡΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΡ (Notyf)
FetchIt “ΠΈΠ· ΠΊΠΎΡΠΎΠ±ΠΊΠΈ” Π½Π΅ Π½Π°Π²ΡΠ·ΡΠ²Π°Π΅Ρ ΡΡΠΈΠ»ΠΈ ΡΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠΉ. ΠΡ ΠΌΠΎΠΆΠ΅ΡΠ΅ ΠΏΠΎΠ΄ΠΊΠ»ΡΡΠΈΡΡ Π»ΡΠ±ΡΡ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΡ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, Notyf.
// ΠΠΎΠ΄ΠΊΠ»ΡΡΠΈΡΠ΅ JS ΠΈ CSS Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠΈ Notyf Π² ΡΠ°Π±Π»ΠΎΠ½Π΅ Π·Π°ΡΠ°Π½Π΅Π΅
const notyf = new Notyf();
document.addEventListener('fetchit:success', (e) => {
const { response } = e.detail;
// response.message ΠΏΡΠΈΡ
ΠΎΠ΄ΠΈΡ ΠΈΠ· ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ° &successMessage
notyf.success(response.message || 'Π€ΠΎΡΠΌΠ° ΡΡΠΏΠ΅ΡΠ½ΠΎ ΠΎΡΠΏΡΠ°Π²Π»Π΅Π½Π°!');
});
document.addEventListener('fetchit:error', (e) => {
const { response } = e.detail;
// response.message ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ &validationErrorMessage ΠΈΠ»ΠΈ ΠΎΡΠΈΠ±ΠΊΡ Ρ
ΡΠΊΠ°
notyf.error(response.message || 'ΠΡΠΎΠΈΠ·ΠΎΡΠ»Π° ΠΎΡΠΈΠ±ΠΊΠ°.');
});
Processing with a Custom Snippet
If you need to process data more complex than FormIt allows, you can specify your snippet in the &snippet parameter. The $FetchIt object is available within it for returning standardized responses.
Call:
[[!FetchIt?
&form=`myForm`
&snippet=`myCustomHandler`
]]
Π‘Π½ΠΈΠΏΠΏΠ΅Ρ myCustomHandler:
<?php
// Fetching data
$name = $_POST['name'] ?? '';
// Basic validation
if (empty($name)) {
// Returning an error. The second argument is an array of field errors to highlight.
return $FetchIt->error('Validation error', [
'name' => 'The name field is required'
]);
}
// Some logic (writing to the database, API request...)
// Returning success
return $FetchIt->success('Everything went fine!', [
'custom_data' => 'Some data for JS'
]);
Working with files
FetchIt automatically handles file uploads. You don’t need to manually assemble the FormData object or specify specific headersβthe component will do it automatically.
To send attachments:
- Add the
enctype="multipart/form-data"attribute to the form tag. - Add the
<input type="file" name="attachment">field. - In the snippet call, don’t forget to specify the attachment hook, for example, the standard
email(FormIt will automatically process the files and attach them to the email if the field name matches the settings).
<form action="[[~[[*id]]]]" method="post" enctype="multipart/form-data">
<!-- Other fields -->
<div class="form-group">
<label>Attach file:</label>
<input type="file" name="file" class="form-control-file">
</div>
<button type="submit">Submit</button>
</form>
Migrating from AjaxForm
If you’re used to using AjaxForm, switching to FetchIt will be painless. The main differences concern JavaScript events.
| Functionality | AjaxForm (jQuery) | FetchIt (Vanilla JS) |
|---|---|---|
| Before-submit event | af_complete (partial) |
fetchit:before |
| Success event | af_complete (with status check) |
fetchit:success |
| Event Errors | af_complete (with status check) |
fetchit:error |
| Form Reset | Parameter &frontend_reset |
Parameter &clearFieldsOnSuccess |
Example: Integration with SweetAlert2
You can use beautiful modal windows instead of the standard alert() or Notyf. Example of connecting SweetAlert2:
// Handling a successful submission
document.addEventListener('fetchit:success', (e) => {
const { response } = e.detail;
Swal.fire({
title: 'Great!',
text: response.message,
icon: 'success',
confirmButtonText: 'Close'
});
});
// Error Handling
document.addEventListener('fetchit:error', (e) => {
const { response } = e.detail;
Swal.fire({
title: 'Error!',
text: response.message || 'Check that the fields are filled in correctly',
icon: 'error',
confirmButtonText: 'OK'
});
});
Conclusion
FetchIt is the “gold standard” for forms in modern MODX. It frees your site from legacy jQuery, speeds up page load times, and gives the developer full control over the data submission process via a convenient JS API.
Use it for contact forms, callback forms, calculators, and any other interactive elements where speed and flexibility are important.






