Ratufa Documentation

Forms with their own validation or submit script

Your form page may already have a script of its own: one that checks the input, shows error messages, saves the submission somewhere or shows a "thank you". Sites made with an AI site builder (Lovable, Bolt and others) and with React, Vue, Svelte or Angular usually do.

Ratufa works together with that script. Your page goes first, Ratufa second:

  1. The visitor submits the form.
  2. Your page's own script runs as it always did.
  3. Ratufa then checks the input against the form's validations and stores the submission, unless your page refused the form.
  4. Ratufa shows its "Thank You" message, unless your page has already answered the visitor (it removed, hid or emptied the form).

The form does not have to be in the page when it loads. Ratufa waits for the form to appear, and follows it when the page renders it again.

Tell Ratufa when your page refuses the form

Validations written as HTML attributes need nothing more. Ratufa reads them from the form and applies them itself:

<input type="email" name="email" required maxlength="255">

If your page checks the input in a script, it has to tell Ratufa when it refuses the form. Otherwise Ratufa stores the input that your page refused. Any one of these three does it.

1. Return false from onsubmit

<form onsubmit="return validateForm(this)">

When validateForm returns false, Ratufa does not send the form.

2. Mark the field that failed

<input name="email" aria-invalid="true">

Form libraries such as shadcn/ui and react-hook-form set aria-invalid on a field that failed their check. Ratufa does not send a form that has such a field. Remove the attribute, or set it to false, when the field is corrected.

3. Give the form a validator

<script>
document.querySelector("form").validator = {
    validate: function () {
        return myValidation();   // true: send the form, false: do not
    }
};
</script>

Ratufa calls validate() on every submit and sends the form only when it returns true. The function has to answer at once: it cannot wait for a server. Use this when the other two do not fit.

Note

  • A form with nothing entered at all is never sent.
  • Only the answer false from onsubmit stops the form. A script that returns nothing, or that fails with an error, does not.
  • aria-invalid has to be set right away, within 50 milliseconds of the submit.

Show your own confirmation

When Ratufa has stored a submission, it fires the event ratufa:success on the form:

<script>
document.addEventListener("ratufa:success", function (e) {
    console.log(e.detail.submission_id);
    e.preventDefault();   // Ratufa then shows no message of its own
});
</script>

If an AI site builder made your site

Paste this to the builder together with the Ratufa script tag:

Keep the form's own validation. When validation fails, set aria-invalid="true" on each field that failed, and remove it when the field is corrected. Do not remove or change the Ratufa script tag.