Reputation: 41
Inertia Does not Returning any validation errors
Here is my controller code
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
Request::validate([
'firstName' => ['required', 'string', 'max:100'],
'lastName' => ['required', 'string', 'max:100'],
'shopName' => ['required', 'string', 'max:100'],
'email' => ['required', 'email', Rule::unique('vendors', 'email')],
'mobile' => ['required', 'string'],
'password' => ['required', 'min:6', 'max:12', 'confirmed']
]);
Vendor::create($request->validated());
return Redirect::route('vendors.create')->with('success', 'Vendor created.');
}
Here is my vue component code
export default {
name: "Create Vendor",
props: {
auth: Object,
errors: Object,
},
data() {
return {
form: this.$inertia.form({
firstName: null,
lastName: null,
shopName: null,
email: null,
mobile: null,
password: null,
password_confirmation: null,
}),
};
},
methods: {
store() {
return this.form.post(this.route("vendors.store"));
},
},
};
But after submit the error object is always empty
Note: I have also set the HandleInertiaRequest middleware.
Upvotes: 4
Views: 3797
Reputation: 391
That's probably because of HandleInertiaRequests
, like Kerem K said. Depending on how your application is structured you'll need to try one of these options:
A - Go to bootstrap/app.php
and make sure the middleware is included in the web stack:
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\App\Http\Middleware\HandleInertiaRequests::class, // this one
\Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
]);
// Other middleware if needed
});
B - In app/Http/Kernel.php
, make sure HandleInertiaRequests
is included in the web middleware group:
'web' => [
// Other middleware
\App\Http\Middleware\HandleInertiaRequests::class,
],
Alternatively, you can apply the middleware only to Inertia routes by defining an alias and applying it to your routes:
/**
* The application's middleware aliases.
*
* Aliases may be used instead of class names to conveniently assign middleware to routes and groups.
*
* @var array<string, class-string|string>
*/
protected $middlewareAliases = [
'inertia' => \App\Http\Middleware\HandleInertiaRequests::class,
];
Route::middleware(['web', 'inertia'])->group(function () {
// Define your Inertia routes here
});
The HandleInertiaRequests
middleware is responsible for sharing Inertia-specific data with your views such as errors. Without this middleware on your route validation errors won’t be passed as props.
Upvotes: 0
Reputation: 1
Check register
method in app/Providers/AppServiceProvider.php
file.
You should have a Inertia::share
like this.
Inertia::share('errors', function () {
return session()->get('errors') ? session()->get('errors')->getBag('default')->getMessages() : (object) [];
});
Upvotes: 0
Reputation: 38
Please be sure these things
Upvotes: 1