Great question! This is a common scenario when using Inertia.js with Vue and Laravel.
Short Answer:
You can still use Inertia's useForm and related helpers for AJAX-style requests (like submitting a modal form and not navigating away), without needing to drop down to Axios and lose Inertia's error handling and reactivity.
Explanation
Inertia is not just for full-page transitions.
You can use Inertia's form helpers (useForm) to submit forms via AJAX and handle validation errors, even if you don't want to navigate away or close a modal. The trick is to use the preserveState and preserveScroll options, and to control what happens after submission.
Example Solution
Suppose you have a modal form. Instead of using Axios, you can do:
import { useForm } from '@inertiajs/vue3'
const form = useForm({
name: '',
email: '',
// ...other fields
})
function submit() {
form.post(route('your.route.name'), {
preserveScroll: true,
onSuccess: () => {
// Optionally reset the form or show a success message
// form.reset()
// Don't close the modal if you don't want to
},
onError: () => {
// Errors are now available in form.errors
// Your InputError components will work as expected
}
})
}
In your template:
<form @submit.prevent="submit">
<input v-model="form.name" />
<InputError :message="form.errors.name" />
<!-- ... -->
<button type="submit">Save</button>
</form>
Key Points:
form.errorswill contain validation errors from Laravel, just like with full-page Inertia requests.- You don't have to close the modal or navigate away unless you want to.
- You get all the benefits of Inertia's form helpers, including error handling and reactivity.
When to Use Axios Directly?
- If you're hitting a non-Inertia endpoint (e.g., a third-party API).
- If you need to upload files and want more control (though Inertia supports file uploads too).
- If you want to handle everything manually.
But for most CRUD forms, stick with Inertia's helpers for a consistent experience.
Summary
- You are not missing anything obvious.
- Inertia's form helpers are designed for both page transitions and AJAX-style requests.
- Use
useFormand related helpers for modal forms, and you keep all the error handling and reactivity.
References:
Let me know if you want a more detailed example!