We use cookies to ensure you get the best experience on our website. Read our privacy policy

Skip to main content

Directory

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.

  • Check Circle

    Bot Protection: Built-in honeypot field

  • Check Circle

    Real-time Validation: Client-side input validation

  • Check Circle

    Data Persistence: Optional auto-save functionality

Create forms with minimal configuration:

Name *
Email *
<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>

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.

Custom Auto-save
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,
});
};
}
Local Data Key
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;
}
Generic Auto-save
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'],
});
}
Required Props
Form Component Extends <form> HTMLElement
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

The Form component provides the following functions:

Form Component Extends <form> HTMLElement
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.

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;
};
}
  • Check Circle Use a unique name for each form: This helps with debugging and identifying the form in local storage
  • Check Circle Check Sensitive Data Is Protected: Make sure to check that sensitive data is protected from being saved to local storage
  • Check Circle Do not add too many fields: Too many fields can overwhelm the user and make the form feel cluttered. Consider using a multi-step form instead.

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!
North Star Themes Logo

Subscribe to our newsletter

Get the latest AstroJS tips and tricks right to your inbox

Email: [email protected]

© 2025 North Star Themes

Web Kit Provided By North Star Themes