Directory
- Welcome
Standard Form Component
The Standard Form component provides a secure, accessible foundation for building forms in your AstroJS application. It includes built-in bot protection, real-time data handling, and comprehensive validation support.
Key Features
-
Bot Protection: Built-in honeypot field
-
Real-time Validation: Client-side input validation
-
Data Persistence: Optional auto-save functionality
Basic Usage
Create forms with minimal configuration:
<Form name="contact-form"> <Input type="text" name="name" required placeholder="Name" label="Name" /> <Input type="email" name="email" required placeholder="Email" label="Email" /></Form>
Auto-save Functionality
Implement auto-save functionality with debouncing. You can set the debounce rate in milliseconds, and define the function that runs on form auto-save. There’s three ways to set this up.
import { ready } from '@/components/NST-components/base';import type { FormElement } from '@/components/NST-components/UI/form/data';import { getData, setData } from '@/lib/client/storage';
const contactForm = await ready( document.querySelector<FormElement>('form-wrapper[name="contact-form"]'));
if (contactForm) { contactForm.saveDataHandler = (formData) => { const contactFormData = { name: formData.get('name')?.toString(), email: formData.get('email')?.toString(), message: formData.get('message')?.toString(), };
setData('contact-form', { ...getData('contact-form'), ...contactFormData, }); };}
import { ready } from '@/components/NST-components/base';import type { FormElement } from '@/components/NST-components/UI/form/data';
const localDataKey = 'contact-form';
const contactForm = await ready( document.querySelector<FormElement>('form-wrapper[name="contact-form"]'));
if (contactForm) { //Can also be set in the forms component props without needing a script contactForm.localDataKey = localDataKey;}
import { ready } from '@/components/NST-components/base';import type { FormElement } from '@/components/NST-components/UI/form/data';import { genericSaveDataHandler } from '@/lib/client/storage';
const localDataKey = 'contact-form';
const contactForm = await ready( document.querySelector<FormElement>('form-wrapper[name="contact-form"]'));
if (contactForm) { contactForm.saveDataHandler = genericSaveDataHandler(localDataKey, { dontSave: ['add_to_newsletter'], });}
Form Component Props API
Prop | Type | Default | Description |
---|---|---|---|
name | string | - | Unique identifier for the form |
debounceRate | number | 500 | Milliseconds to debounce input events |
honeypot | boolean | true | Enable/disable bot protection |
genericErrorMessage | string | An error occurred | Custom generic error message |
localDataKey | keyof LocalDataSchemas | Key to save form data to local storage | |
submitButton | boolean | true | Show default submit button |
submitButtonProps | ComponentProps<typeof Button> | {} | Props for the submit button |
submitButtonText | string | Submit | Custom submit button text |
submitButtonTextSubmitting | string | Submitting... | Custom submitting button text |
successMessage | string | Submission successful | Custom success message |
wrapperProps | HTMLAttributesWithData<div> | {} | Props for the form wrapper element |
Form Functions API
The Form component provides the following functions:
Name | Type | Description |
---|---|---|
loadData | (data: Record<string, unknown>) => void | Loads the data into the form, key should match the name of the input fields |
get: data | FormData | Returns the form data |
set: localDataKey | keyof LocalDataSchemas | Key to save and load form data in local storage |
set: submitHandler | ActionHandler | Sets the function that runs on form submission |
set: saveDataHandler | SaveDataHandler | Sets the function that runs on form auto-save |
The functions submitHandler
, saveDataHandler
, and loadData
should be defined after the form is initialized in a client-side script.
Submit Handler Example
import { getData } from '@/lib/client/storage';import { actions } from 'astro:actions';import type { FormElement } from '@/components/NST-components/UI/form/data';import type { ToastElement } from '@/components/NST-components/UI/toast/data';import { ready } from '@/components/NST-components/base';
const contactForm = await ready( document.querySelector<FormElement>('form-wrapper[name="contact-form"]'));
const toast = await ready( document.querySelector<ToastElement>('#contact-form-toast'));
if (contactForm) { const userContactHistory = getData('contact-form'); if (userContactHistory) { contactForm.loadData(userContactHistory); }
contactForm.submitHandler = async (formData) => { const { error } = await actions['form/contact'](formData);
if (!error) { toast?.show({ message: 'Message sent successfully', duration: 3000, variant: 'success', }); }
return error; };}
Best Practices
Limited Time Launch Sale
Start building secure, accessible forms today with our AstroJS starter template. Get instant access to the Form component and more!
GET 60% OFF!