Form
Form
creates a form with method
set to POST
and a couple of enhancements.
-->
Form
creates a form with method
set to POST
and a couple of enhancements.
Most of the forms we build use POST
(over GET
), so we made it a default to save a couple of keystrokes every time.
<Form> ... </Form>
<form method="post">...</form>
Form
contains a hidden
input to make it a tiny bit easier to prevent spam. To disable this feature, just set the preventSpam
option to false
.
<Form> ... </Form>
<form method="post"> <input type="hidden" name="spam" /> ...</form>
If you provide Form
with a onSubmit
handler, Form
will use JavaScript to parse and sanitize data for you — without you having to lift a finger.
<script> function onSubmit (data) { // Do something with your data }</script>
<Form {onSubmit}> ... </Form>
{
name: ""
}
Form
contains a helper that lets you send an array of objects through your forms. This is useful when you need to send multiple items of the same type.
<Form {onSubmit}> <TextInput name="members[0][name]" label="Member 1 Name" /> <TextInput name="members[1][name]" label="Member 2 Name" /> <TextInput name="members[2][name]" label="Member 3 Name" /> <button type="submit">Submit</button></Form>
{
}
Form
creates a <form>
element that does the following:
method
to POST
<script> import { Form } from '@splendidlabz/svelte'</script>
<Form>...</Form>
<form method="post"> <input type="hidden" name="spam" /> ...</form>
Method and other <form>
attributes can be directly added to the Form
.
<TextInput name="name" label="First Name"> ... </TextInput>
<form method="get" action="/somewhere"> <input type="hidden" name="spam" /> ...</form>
If you don’t want to use the spam prevention feature, set preventSpam
to false
.
<Form options={{ preventSpam: false }}> ... </Form>
<form method="post">...</form>
Form
will submit the form via the standard HTML way if onSubmit
is not present.
<!-- Standard Submit --><Form>...</Form>
You can intercept a form submission by providing a onSubmit
callback. When you do this, Form
will:
onSubmit
callback<script> function onSubmit(data) { // Do something with your data here }</script>
<Form {onSubmit}> ... </Form>
{
name: ""
}
For Astro users: You can’t pass an onSubmit
function into Form
from an .astro
file. This is an Astro limitation.
If you want to benefit from the parsing enhancements we added here, consider using a Svelte component to handle the form submission or, use the parseData
server helper.
You can disable the sanitization of form data by setting the sanitize
option to false
.
<script> function onSubmit(data) { // Do something with your data here }</script>
<Form {onSubmit} options={{ sanitize: false }}> ... </Form>
{
name: ""
}
Form
includes a helper to send an array of objects. To activate this, make sure you have onSubmit
. Then, use the following syntax for naming your inputs:
<input name="property[index][nestedProperty]" />
Here’s an example of this at work:
<Form {onSubmit}> <TextInput name="members[0][name]" label="Member 1 Name" /> <TextInput name="members[1][name]" label="Member 2 Name" /> <TextInput name="members[2][name]" label="Member 3 Name" /> <button type="submit">Submit</button></Form>
{
}
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Form
creates a <form>
element that does the following:
method
to POST
<script> import { Form } from '@splendidlabz/svelte'</script>
<Form>...</Form>
<form method="post"> <input type="hidden" name="spam" /> ...</form>
Method and other <form>
attributes can be directly added to the Form
.
<TextInput name="name" label="First Name"> ... </TextInput>
<form method="get" action="/somewhere"> <input type="hidden" name="spam" /> ...</form>
If you don’t want to use the spam prevention feature, set preventSpam
to false
.
<Form options={{ preventSpam: false }}> ... </Form>
<form method="post">...</form>
Form
will submit the form via the standard HTML way if onSubmit
is not present.
<!-- Standard Submit --><Form>...</Form>
You can intercept a form submission by providing a onSubmit
callback. When you do this, Form
will:
onSubmit
callback<script> function onSubmit(data) { // Do something with your data here }</script>
<Form {onSubmit}> ... </Form>
{
name: ""
}
For Astro users: You can’t pass an onSubmit
function into Form
from an .astro
file. This is an Astro limitation.
If you want to benefit from the parsing enhancements we added here, consider using a Svelte component to handle the form submission or, use the parseData
server helper.
You can disable the sanitization of form data by setting the sanitize
option to false
.
<script> function onSubmit(data) { // Do something with your data here }</script>
<Form {onSubmit} options={{ sanitize: false }}> ... </Form>
{
name: ""
}
Form
includes a helper to send an array of objects. To activate this, make sure you have onSubmit
. Then, use the following syntax for naming your inputs:
<input name="property[index][nestedProperty]" />
Here’s an example of this at work:
<Form {onSubmit}> <TextInput name="members[0][name]" label="Member 1 Name" /> <TextInput name="members[1][name]" label="Member 2 Name" /> <TextInput name="members[2][name]" label="Member 3 Name" /> <button type="submit">Submit</button></Form>
{
}
Form
produces the following HTML:
<form class="Form" method="post"> <input type="hidden" name="spam" /> ...</form>
The following properties can be used to add classes to Form
.
Property | Description |
---|---|
class | Class for the <form> |
styles | Inline styles for the <form> |
<Form class="Yay">...</Form>
<form class="Form Yay" method="post">...</form>
All other custom properties will be passed directly to <form>
.
<Form data-extra="form">...</Form>
<form method="post" data-extra="form">...</form>
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Form
produces the following HTML:
<form class="Form" method="post"> <input type="hidden" name="spam" /> ...</form>
The following properties can be used to add classes to Form
.
Property | Description |
---|---|
class | Class for the <form> |
styles | Inline styles for the <form> |
<Form class="Yay">...</Form>
<form class="Form Yay" method="post">...</form>
All other custom properties will be passed directly to <form>
.
<Form data-extra="form">...</Form>
<form method="post" data-extra="form">...</form>
Name | Description |
---|---|
Default | HTML to insert into the <form> . |
Property | Type | Description |
---|---|---|
method | string | HTTP method for form submission |
action | string | URL to submit the form data |
onSubmit | function | Callback function triggered on form submission |
options | object | Form configuration. See Options |
Configuration object for Form
component.
Property | Type | Default | Description |
---|---|---|---|
sanitize | boolean | true | Sanitize form inputs before passing to onSubmit . |
preventSpam | boolean | true | Include a hidden input with name="spam" for spam prevention. |
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Name | Description |
---|---|
Default | HTML to insert into the <form> . |
Property | Type | Description |
---|---|---|
method | string | HTTP method for form submission |
action | string | URL to submit the form data |
onSubmit | function | Callback function triggered on form submission |
options | object | Form configuration. See Options |
Configuration object for Form
component.
Property | Type | Default | Description |
---|---|---|---|
sanitize | boolean | true | Sanitize form inputs before passing to onSubmit . |
preventSpam | boolean | true | Include a hidden input with name="spam" for spam prevention. |