AjaxFormItLogin
AjaxFormItLogin is a modern take on the AjaxForm component. It keeps all original features and adds more.
Features
No jQuery dependency.
Uses IziToast for notifications.
clearFieldsOnSuccess— control whether form fields are cleared after a successful submit.transmittedParams(valid JSON) — pass custom params to JS separately for success and error.showUploadProgress(1) — show file upload progress.Event
af_completeis replaced byafl_complete.jsdocument.addEventListener('afl_complete', e => { console.log(e.detail.response); // server response console.log(e.detail.form); // current form });Server response format:
e.detail.response.dataincludeserrors(validation errors when form is not submitted), plus any params you pass viatransmittedParams. Pass either JSON or an object with keyssuccessand/orerror(comma-separated snippet param names to pass to JS). On success, response also includese.detail.response.data.redirectTimeoutande.detail.response.data.redirectUrl.Redirects: set snippet param
redirectTo(URL or resource ID) and optionallyredirectTimeout(ms, default 2000).Checkbox validation: add
data-afl-required(value = key fromvalidate) to the checkbox and a hidden field with that name in the form. The checkbox itself can be unnamed.No Google reCAPTCHA; built-in spam protection by Alexey Smirnov. Set
spamProtectionto 1 in the snippet call. On by default.Registration, login, password reset, and profile editing when FormIt is installed. See this note for supported params.
On user profile update, system event aiOnUserUpdate fires with $user (updated user), $profile (profile), $data (submitted data).
Yandex.Metrika goals: set system setting
afl_metricsto Yes andafl_counter_idto your counter ID, add the counter code to the site, and in the snippet call setym_goal(JS goal name) andtransmittedParamsto['success' => 'ym_goal'].You can still use your own snippets for form handling; only the API object and response format changed. Example snippet
MySnippetthat checks fieldname:
<?php
if (empty($_POST['name'])) {
return $AjaxFormItLogin->error('Form errors', array(
'errors' => array(
'name' => 'Please enter your name.'
)
));
}
else {
return $AjaxFormItLogin->success('Form validated.', array('name' => 'Name is valid.'));
}Call example:
{'!AjaxFormItLogin' | snippet: [
'snippet' => 'MySnippet',
'tpl' => 'myTpl',
]}Quick start
Install AjaxFormItLogin from https://modstore.pro. FormIt will be installed automatically if missing.
Copy everything between
<form></form>(including the tags) into a chunk. Remember the chunk name.Replace the form on the page (not in the chunk) with one of these calls:
modx[[!AjaxFormitLogin? &form=`aflExampleForm` &emailTo=`name@domain.ru` &emailFrom=`noreply@domain.ru` &emailSubject=`Email subject` &emailTpl=`aflExampleEmail` &validate=`email:required:email,name:required:minLength=^3^,phone:required,politics:minValue=^1^` ]]fenom{'!AjaxFormItLogin' | snippet: [ 'form' => 'aflExampleForm', 'hooks' => 'FormItSaveForm,email', 'emailTo' => 'recipient@example.com', 'emailFrom' => 'noreply@example.com', 'emailSubject' => 'Email subject', 'emailTpl' => 'aflExampleEmail', 'validate' => 'email:required:email,name:required:minLength=^3^,phone:required,politics:minValue=^1^', ]}Подсказка
For email params, hooks, and validation see FormIt documentation; AjaxFormItLogin supports all FormIt params.
Example form:
html<form id="aflExampleForm"> <div class="mb-3"> <label class="form-label">Name</label> <input type="text" class="form-control" name="name" placeholder="John Doe"> <small class="text-danger d-block error_name"></small> </div> <div class="mb-3"> <label class="form-label">Phone</label> <input type="tel" name="phone" class="form-control" placeholder="+7(999)123-45-67"> <small class="text-danger d-block error_phone"></small> </div> <div class="mb-3"> <label class="form-label">Email</label> <input type="email" name="email" class="form-control" placeholder="name@mail.ru"> <small class="text-danger d-block error_email"></small> </div> <div class="mb-3 form-check"> <input type="hidden" name="politics" value="0"> <input type="checkbox" class="form-check-input" id="politics" data-afl-required="politics"> <label class="form-check-label" for="politics">I accept the site <a href="#" target="_blank">terms of use</a>.</label> <small class="text-danger d-block error_politics"></small> </div> <button type="submit" class="btn btn-primary">Submit</button> <button type="reset" class="btn btn-secondary">Reset</button> </form>Осторожно
You do not have to add
error_fieldnameblocks for validation errors; if you omit them, errors show as a toast in the formatfieldname: message. To show custom labels instead of field names, set snippet paramsaliases(e.g.fieldname==Display text) andtransmittedParamsto['error' => 'aliases'].Checkbox validation block:
html<input type="hidden" name="politics" value="0"> <input type="checkbox" class="form-check-input" id="politics" data-afl-required="politics">The checkbox must be checked; in
validatewe requirepoliticsto be greater than 0.Create an email chunk (e.g. aflExampleEmail). Example:
modx[[+name:isnot=``:then=`<p>Name: [[+name]]</p>`]] [[+phone:isnot=``:then=`<p>Phone: [[+phone]]</p>`]] [[+email:isnot=``:then=`<p>Email: [[+email]]</p>`]]fenom{$name ? '<p>Name: ' ~ $name ~ '</p>' : ''} {$phone ? '<p>Phone: ' ~ $phone ~ '</p>' : ''} {$email ? '<p>Email: ' ~ $email ~ '</p>' : ''}
Осторожно
The example uses database chunks; the component also supports file chunks. Using file chunks with pdoTools parser is recommended. Your form will then save data in the manager and send email.
Example calls and chunks are in core/components/ajaxformitlogin/elements/templates/ and public_html/core/components/ajaxformitlogin/elements/chunks/.
