Summary
I have a simple sign up form in a modal window. The Formit call works if it is on a standalone resource, but I want the modal to display a success message when the form is submitted or show validation errors without having to reload the page (which closes the modal).
I looked for an easy AJAX solution, which I found in HTMX, the form submits and displays the relevant success message. However the FormIt validation is completely ignored and the errors not shown.
Is it possible to still use HTMX and get it to use the FormIt Validation?
I came across the SepiaRiver article MODX Forms via AJAX, but both it and the GitHub link in the article presume too much knowledge on the part of the reader and I canāt work out how to use it, or even if it would help me with a solution.
The call that I am using to Formit and the form looks like this:
[[!FormIt?
&form=`sign-up-form`
&hooks=`spam,FormItSaveForm,sendgrid-add-contact`
&formName=`New Contact`
&successMessage=`You're Awesome`
&validationErrorMessage=`Oops!`
&validate=`nospam:blank,
preferred-name:regexp=^/^[a-zA-Z -]*$/^:required,
first-name:regexp=^/^[a-zA-Z -]*$/^:required,
last-name:regexp=^/^[a-zA-Z -]*$/^:required,
email:email:required`
]]
<form id="sign-up" hx-post="[[~[[*id]]]]" hx-target="#sign-up" hx-select="#sign-up" class="form mulish-light" enctype="multipart/form-data">
[[+fi.success:is=`1`:then=`
<div class="form-notification success-main">
<span class="form-notification-title caveat-brush-regular">[[+fi.successMessage]]</span>
<span class="form-notification-body">Thanks for joining. Quick, your email needs some "you-time". Your freebie awaits!</span>
</div>
`]]
[[+fi.validation_error:is=`1`:then=`
<div class="form-notification error-main">
<span class="form-notification-title caveat-brush-regular">[[+fi.validation_error_message]]</span>
<span class="form-notification-body">Something's not right! Check over your details again so we can get you signed up as quickly as the brown fox jumps over the lazy dog!</span>
</div>
`]]
[[+fi.success:is=`1`:then=`
`:else`
<input type="hidden" name="nospam:blank" value="[[+fi.nospam]]" />
<section id="form-body">
<p [[!+fi.error.preferred-name:notempty=`class="has-error"`]]>
<label>
<span>Preferred Name:</span>
[[!+fi.error.preferred-name:notempty=`<span class="error">Hmmm, just letters please.</span>`]]
<input type="text" class="mulish-light" name="preferred-name" id="preferred-name" value="[[!+fi.preferred-name]]" placeholder="Your preferred name" autofocus required />
<!--<span class="input-error" data-error="preferred-name">[[+fi.error.preferred-name]]</span>-->
</label>
</p>
<p [[!+fi.error.first-name:notempty=`class="has-error"`]]>
<label>
<span>First Name:</span>
[[!+fi.error.first-name:notempty=`<span class="error">Letters and letters only please.</span>`]]
<input type="text" class="mulish-light" name="first-name" id="first-name" value="[[!+fi.first-name]]" placeholder="Your first name" autofocus required />
<!--<span class="input-error" data-error="first-name">[[+fi.error.first-name]]</span>-->
</label>
</p>
<p[[!+fi.error.last-name:notempty=`class="has-error"`]]>
<label>
<span>Last Name:</span>
[[!+fi.error.last-name:notempty=`<span class="error">Do you have punctuation in your name? I didn't think so.</span>`]]
<input type="text" class="mulish-light" name="last-name" id="last-name" value="[[!+fi.last-name]]" placeholder="Your last name" required />
<!--<span class="input-error" data-error="last-name">[[+fi.error.last-name]]</span>-->
</label>
</p>
<p[[!+fi.error.email:notempty=`class="has-error"`]]>
<label>
<span>E-mail:</span>
[[!+fi.error.email:notempty=`<span class="error">Well, this doesn't look like an email address. Best check it.</span>`]]
<input type="email" class="mulish-light" name="email" id="email" placeholder="someone@something.com" value="[[!+fi.email]]" required />
<!--<span class="input-error" data-error="email">[[+fi.error.email]]</span>-->
</label>
</p>
</section>
<input type="submit" class ="lb-button-yellow caveat-brush-regular" name="submit" id="submit" value="Join now"/>
`]]
</form>
Any help would be greatly appreciated, as otherwise I feel like I will need to revert to having a link in the modal to a separate resource in order to process the form.