Level 25
Hi @timny here is the video lesson on how to handle validation errors with inertia: https://laracasts.com/series/build-modern-laravel-apps-using-inertia-js/episodes/20
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have this script file, on submitting the form I want it to validate the inputs and respond respectively, kinda stuck, please help
BusinessRegister.vue
<script setup>
import { Head, useForm } from "@inertiajs/vue3";
import Authenticated from "@/layouts/Authenticated.vue";
import { ref } from "vue";
defineOptions({
layout: Authenticated,
});
const user = useForm({
personal: {
full_name: null,
email: null,
password: null,
confirmPassword: null,
phone: null,
},
business: {
business_name: null,
location: null,
work_category: null,
services: null,
},
});
const activeStep = ref(1);
async function checkPersonal(activateCallback) {
// Include the step in the payload
await user.post(
route('validate-step'),
{
step: 1, // Include the current step in the payload
personal: user.personal,
},
{
onError: (errors) => {
// Handle validation errors
user.errors.personal = errors.personal || {};
},
onSuccess: () => {
// Clear errors and move to the next step if validation is successful
user.errors.personal = {};
activateCallback(2); // Move to the next step
},
preserveScroll: true,
preserveState: true,
}
);
}
</script>
routes/web.php
Route::post('validate-step', [BusinessRegisterController::class, 'validateStep'])->name('validate-step');
BusinessRegisterController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Inertia\Inertia;
class BusinessRegisterController extends Controller
{
public function validateStep(Request $request): JsonResponse
{
$step = $request->input('step'); // Get the step from the request
switch ($step) {
case 1:
return $this->validatePersonalDetails($request);
case 2:
return $this->validateBusinessDetails($request);
default:
return response()->json(['message' => 'Invalid step'], 400);
}
}
protected function validatePersonalDetails(Request $request): JsonResponse
{
$validator = Validator::make($request->input('personal', []), [
'full_name' => 'required|string|max:255',
'email' => 'required|email|max:255',
'password' => 'required|string|min:8|confirmed',
'phone' => 'required|string|min:10|max:15',
]);
if ($validator->fails()) {
// Return errors back to the front-end via JSON
return response()->json([
'errors' => $validator->errors(),
'user' => $request->input('personal'),
], 422); // 422 Unprocessable Entity
}
return response()->json(['message' => 'Personal details are valid'], 200);
}
protected function validateBusinessDetails(Request $request)
{
// Implement business details validation logic here
}
}
Please or to participate in this conversation.