Laravel Inertia not returning validation errors

Viewed 1363

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.

enter image description here

2 Answers

Please be sure these things

  • In the HandleInertiaRequests.php file the Middleware comes from use Inertia\Middleware; instead of Laravel Foundation
  • In Http/Kernel.php you add \App\Http\Middleware\HandleInertiaRequests::class to the $middlewareGroups for collapse all web routes.

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) [];
});
Related