@@ -18,9 +18,13 @@ export default function () {
18
18
return (
19
19
< >
20
20
< H1 > Forms</ H1 >
21
+ < P >
22
+ Inertia provides two primary ways to build forms: the < Code > <Form></ Code > component and the < Code > useForm</ Code > helper.
23
+ Both integrate with your server-side framework's validation and handle form submissions without full page reloads.
24
+ </ P >
21
25
< H2 > Form component</ H2 >
22
26
< P >
23
- Inertia provides a < Code > <Form></ Code > component that behaves much like a classic HTML form, but uses
27
+ Inertia provides a < Code > <Form></ Code > component that behaves much like a classic HTML form, but uses
24
28
Inertia under the hood to avoid full page reloads. This is the simplest way to get started with forms in Inertia:
25
29
</ P >
26
30
< TabbedCode
@@ -158,7 +162,7 @@ export default function () {
158
162
/>
159
163
< H3 > Slot props</ H3 >
160
164
< P >
161
- The < Code > <Form></ Code > component exposes reactive state and helper methods through its default slot,
165
+ The < Code > <Form></ Code > component exposes reactive state and helper methods through its default slot,
162
166
giving you access to form processing state, errors, and utility functions:
163
167
</ P >
164
168
< TabbedCode
@@ -676,9 +680,97 @@ export default function () {
676
680
} ,
677
681
] }
678
682
/>
683
+ < H3 > Programmatic access</ H3 >
684
+ < P >
685
+ You can access the form's methods programmatically using refs. This provides an alternative to the{ ' ' }
686
+ < A href = "#slot-props" > slot props</ A > approach when you need to trigger form actions from outside the form:
687
+ </ P >
688
+ < TabbedCode
689
+ examples = { [
690
+ {
691
+ name : 'Vue' ,
692
+ language : 'markup' ,
693
+ code : dedent `
694
+ <script setup>
695
+ import { ref } from 'vue'
696
+ import { Form } from '@inertiajs/vue3'
697
+
698
+ const formRef = ref()
699
+
700
+ const handleSubmit = () => {
701
+ formRef.value.submit()
702
+ }
703
+ </script>
704
+
705
+ <template>
706
+ <Form ref="formRef" action="/users" method="post">
707
+ <input type="text" name="name" />
708
+ <button type="submit">Submit</button>
709
+ </Form>
710
+
711
+ <button @click="handleSubmit">Submit Programmatically</button>
712
+ </template>
713
+ ` ,
714
+ } ,
715
+ {
716
+ name : 'React' ,
717
+ language : 'jsx' ,
718
+ code : dedent `
719
+ import { useRef } from 'react'
720
+ import { Form } from '@inertiajs/react'
721
+
722
+ export default function CreateUser() {
723
+ const formRef = useRef()
724
+
725
+ const handleSubmit = () => {
726
+ formRef.current.submit()
727
+ }
728
+
729
+ return (
730
+ <>
731
+ <Form ref={formRef} action="/users" method="post">
732
+ <input type="text" name="name" />
733
+ <button type="submit">Submit</button>
734
+ </Form>
735
+
736
+ <button onClick={handleSubmit}>Submit Programmatically</button>
737
+ </>
738
+ )
739
+ }
740
+ ` ,
741
+ } ,
742
+ {
743
+ name : 'Svelte' ,
744
+ language : 'html' ,
745
+ code : dedent `
746
+ <script>
747
+ import { Form } from '@inertiajs/svelte'
748
+
749
+ let formRef
750
+
751
+ function handleSubmit() {
752
+ formRef.submit()
753
+ }
754
+ </script>
755
+
756
+ <Form bind:this={formRef} action="/users" method="post">
757
+ <input type="text" name="name" />
758
+ <button type="submit">Submit</button>
759
+ </Form>
760
+
761
+ <button on:click={handleSubmit}>Submit Programmatically</button>
762
+ ` ,
763
+ } ,
764
+ ] }
765
+ />
766
+ < P >
767
+ In React and Vue, refs provide access to all form methods and reactive state. In Svelte, refs expose only
768
+ methods, so reactive state like < Code > isDirty</ Code > and < Code > errors</ Code > should be accessed via{ ' ' }
769
+ < A href = "#slot-props" > slot props</ A > instead.
770
+ </ P >
679
771
< H2 > Form helper</ H2 >
680
772
< P >
681
- In addition to the < Code > <Form></ Code > component, Inertia also provides a < Code > useForm</ Code > helper for
773
+ In addition to the < Code > <Form></ Code > component, Inertia also provides a < Code > useForm</ Code > helper for
682
774
when you need programmatic control over your form's data and submission behavior:
683
775
</ P >
684
776
< TabbedCode
@@ -1199,7 +1291,7 @@ export default function () {
1199
1291
] }
1200
1292
/>
1201
1293
< P >
1202
- Sometimes, you may want to restore your form fields to their default values and clear any validation errors at
1294
+ Sometimes, you may want to restore your form fields to their default values and clear any validation errors at
1203
1295
the same time. Instead of calling < Code > reset()</ Code > and < Code > clearErrors()</ Code > separately, you can use the{ ' ' }
1204
1296
< Code > resetAndClearErrors()</ Code > method, which combines both actions into a single call.
1205
1297
</ P >
@@ -1463,8 +1555,8 @@ export default function () {
1463
1555
/>
1464
1556
< H2 > Server-side responses</ H2 >
1465
1557
< P >
1466
- When using Inertia, you don't typically inspect form responses client-side like you would with traditional XHR/fetch
1467
- requests. Instead, your server-side route or controller issues a < A href = "/redirects" > redirect</ A > response after
1558
+ When using Inertia, you don't typically inspect form responses client-side like you would with traditional XHR/fetch
1559
+ requests. Instead, your server-side route or controller issues a < A href = "/redirects" > redirect</ A > response after
1468
1560
processing the form, often redirecting to a success page.
1469
1561
</ P >
1470
1562
< TabbedCode
@@ -1498,19 +1590,19 @@ export default function () {
1498
1590
] }
1499
1591
/>
1500
1592
< P >
1501
- This redirect-based approach works with all form submission methods: the < Code > <Form></ Code > component,
1502
- < Code > useForm</ Code > helper, and manual router submissions. It makes handling Inertia forms feel very similar to
1593
+ This redirect-based approach works with all form submission methods: the < Code > <Form></ Code > component,
1594
+ < Code > useForm</ Code > helper, and manual router submissions. It makes handling Inertia forms feel very similar to
1503
1595
classic server-side form submissions.
1504
1596
</ P >
1505
1597
< H2 > Server-side validation</ H2 >
1506
1598
< P >
1507
- Both the < Code > <Form></ Code > component and < Code > useForm</ Code > helper automatically handle server-side
1599
+ Both the < Code > <Form></ Code > component and < Code > useForm</ Code > helper automatically handle server-side
1508
1600
validation errors. When your server returns validation errors, they're automatically available in the < Code > errors</ Code > { ' ' }
1509
1601
object without any additional configuration.
1510
1602
</ P >
1511
1603
< P >
1512
- Unlike traditional XHR/fetch requests where you'd check for a < Code > 422</ Code > status code, Inertia handles
1513
- validation errors as part of its redirect-based flow, just like classic server-side form submissions, but without
1604
+ Unlike traditional XHR/fetch requests where you'd check for a < Code > 422</ Code > status code, Inertia handles
1605
+ validation errors as part of its redirect-based flow, just like classic server-side form submissions, but without
1514
1606
the full page reload.
1515
1607
</ P >
1516
1608
< P >
@@ -1519,7 +1611,7 @@ export default function () {
1519
1611
</ P >
1520
1612
< H2 > Manual form submissions</ H2 >
1521
1613
< P >
1522
- It's also possible to submit forms manually using Inertia's < Code > router</ Code > methods directly, without using
1614
+ It's also possible to submit forms manually using Inertia's < Code > router</ Code > methods directly, without using
1523
1615
the < Code > <Form></ Code > component or < Code > useForm</ Code > helper:
1524
1616
</ P >
1525
1617
< TabbedCode
@@ -1668,7 +1760,7 @@ export default function () {
1668
1760
< H2 > File uploads</ H2 >
1669
1761
< P >
1670
1762
When making requests or form submissions that include files, Inertia will automatically convert the request data
1671
- into a < Code > FormData</ Code > object. This works with the < Code > <Form></ Code > component, < Code > useForm</ Code > { ' ' }
1763
+ into a < Code > FormData</ Code > object. This works with the < Code > <Form></ Code > component, < Code > useForm</ Code > { ' ' }
1672
1764
helper, and manual router submissions.
1673
1765
</ P >
1674
1766
< P >
0 commit comments