Skip to content

Commit 213d9d8

Browse files
authored
Merge pull request #435 from inertiajs/reset-and-clear-errors
Docs about `resetAndClearErrors()` Form helper
2 parents 0400082 + 56de470 commit 213d9d8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

resources/js/Pages/forms.jsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,50 @@ export default function () {
742742
},
743743
]}
744744
/>
745+
<P>
746+
Sometimes, you may want to restore your form fields to their default values and clear any validation errors at{' '}
747+
the same time. Instead of calling <Code>reset()</Code> and <Code>clearErrors()</Code> separately, you can use the{' '}
748+
<Code>resetAndClearErrors()</Code> method, which combines both actions into a single call.
749+
</P>
750+
<TabbedCode
751+
examples={[
752+
{
753+
name: 'Vue',
754+
language: 'js',
755+
code: dedent`
756+
// Reset the form and clear all errors...
757+
form.resetAndClearErrors()
758+
759+
// Reset specific fields and clear their errors...
760+
form.resetAndClearErrors('field', 'anotherfield')
761+
`,
762+
},
763+
{
764+
name: 'React',
765+
language: 'js',
766+
code: dedent`
767+
const { resetAndClearErrors } = useForm({ ... })
768+
769+
// Reset the form and clear all errors...
770+
resetAndClearErrors()
771+
772+
// Reset specific fields and clear their errors...
773+
resetAndClearErrors('field', 'anotherfield')
774+
`,
775+
},
776+
{
777+
name: 'Svelte',
778+
language: 'js',
779+
code: dedent`
780+
// Reset the form and clear all errors...
781+
$form.resetAndClearErrors()
782+
783+
// Reset specific fields and clear their errors...
784+
$form.resetAndClearErrors('field', 'anotherfield')
785+
`,
786+
},
787+
]}
788+
/>
745789
<P>
746790
If your form's default values become outdated, you can use the <Code>defaults()</Code> method to update them.
747791
Then, the form will be reset to the correct values the next time the <Code>reset()</Code> method is invoked.

0 commit comments

Comments
 (0)