Skip to content

Commit 8c1a80e

Browse files
committed
wip
1 parent e776c6d commit 8c1a80e

File tree

1 file changed

+105
-13
lines changed

1 file changed

+105
-13
lines changed

resources/js/Pages/forms.jsx

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ export default function () {
1818
return (
1919
<>
2020
<H1>Forms</H1>
21+
<P>
22+
Inertia provides two primary ways to build forms: the <Code>&lt;Form&gt;</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>
2125
<H2>Form component</H2>
2226
<P>
23-
Inertia provides a <Code>&lt;Form&gt;</Code> component that behaves much like a classic HTML form, but uses
27+
Inertia provides a <Code>&lt;Form&gt;</Code> component that behaves much like a classic HTML form, but uses
2428
Inertia under the hood to avoid full page reloads. This is the simplest way to get started with forms in Inertia:
2529
</P>
2630
<TabbedCode
@@ -158,7 +162,7 @@ export default function () {
158162
/>
159163
<H3>Slot props</H3>
160164
<P>
161-
The <Code>&lt;Form&gt;</Code> component exposes reactive state and helper methods through its default slot,
165+
The <Code>&lt;Form&gt;</Code> component exposes reactive state and helper methods through its default slot,
162166
giving you access to form processing state, errors, and utility functions:
163167
</P>
164168
<TabbedCode
@@ -676,9 +680,97 @@ export default function () {
676680
},
677681
]}
678682
/>
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>
679771
<H2>Form helper</H2>
680772
<P>
681-
In addition to the <Code>&lt;Form&gt;</Code> component, Inertia also provides a <Code>useForm</Code> helper for
773+
In addition to the <Code>&lt;Form&gt;</Code> component, Inertia also provides a <Code>useForm</Code> helper for
682774
when you need programmatic control over your form's data and submission behavior:
683775
</P>
684776
<TabbedCode
@@ -1199,7 +1291,7 @@ export default function () {
11991291
]}
12001292
/>
12011293
<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
12031295
the same time. Instead of calling <Code>reset()</Code> and <Code>clearErrors()</Code> separately, you can use the{' '}
12041296
<Code>resetAndClearErrors()</Code> method, which combines both actions into a single call.
12051297
</P>
@@ -1463,8 +1555,8 @@ export default function () {
14631555
/>
14641556
<H2>Server-side responses</H2>
14651557
<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
14681560
processing the form, often redirecting to a success page.
14691561
</P>
14701562
<TabbedCode
@@ -1498,19 +1590,19 @@ export default function () {
14981590
]}
14991591
/>
15001592
<P>
1501-
This redirect-based approach works with all form submission methods: the <Code>&lt;Form&gt;</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>&lt;Form&gt;</Code> component,
1594+
<Code>useForm</Code> helper, and manual router submissions. It makes handling Inertia forms feel very similar to
15031595
classic server-side form submissions.
15041596
</P>
15051597
<H2>Server-side validation</H2>
15061598
<P>
1507-
Both the <Code>&lt;Form&gt;</Code> component and <Code>useForm</Code> helper automatically handle server-side
1599+
Both the <Code>&lt;Form&gt;</Code> component and <Code>useForm</Code> helper automatically handle server-side
15081600
validation errors. When your server returns validation errors, they're automatically available in the <Code>errors</Code>{' '}
15091601
object without any additional configuration.
15101602
</P>
15111603
<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
15141606
the full page reload.
15151607
</P>
15161608
<P>
@@ -1519,7 +1611,7 @@ export default function () {
15191611
</P>
15201612
<H2>Manual form submissions</H2>
15211613
<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
15231615
the <Code>&lt;Form&gt;</Code> component or <Code>useForm</Code> helper:
15241616
</P>
15251617
<TabbedCode
@@ -1668,7 +1760,7 @@ export default function () {
16681760
<H2>File uploads</H2>
16691761
<P>
16701762
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>&lt;Form&gt;</Code> component, <Code>useForm</Code>{' '}
1763+
into a <Code>FormData</Code> object. This works with the <Code>&lt;Form&gt;</Code> component, <Code>useForm</Code>{' '}
16721764
helper, and manual router submissions.
16731765
</P>
16741766
<P>

0 commit comments

Comments
 (0)