Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

Using laravel api how to show the validation error in vue js?

my store api controller method

 public function register(Request $request)
    {
        $attributes = $request->validate([
            'name' => 'required',
            'email' => 'required|unique:users,email',
            'password' => 'required|confirmed',
        ]);

        $user = User::create($attributes);


        return response()->json([
            'username' => $user->name
        ]);
    }

my store

my register method

and getting this CORS error in network tab not any preview or anything.

how to get those validaton error ?

1 like
7 replies
vincent15000's avatar

The validation sends automatically the errors if the request is not validated.

Have a look at the response, you will find the errors inside it.

if (!res.ok) {
	console.log(res)
} 
Shivamyadav's avatar

@vincent15000 getting this

Access to fetch at 'http://localhost:5173/' (redirected from 'http://127.0.0.1:8000/api/register') from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
1 like
vincent15000's avatar

@Shivamyadav Hvae you declared the front http address in your sanctum configuration ?

I think that Laravel 12 does this automatically, but with previous versions, you need to specify it yourself in the Sanctum configuration.

'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
    '%s%s',
    'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
    Sanctum::currentApplicationUrlWithPort()
))),
martinbean's avatar

@Shivamyadav You need to take a step back. You need to stop worrying about validation and first fix the CORS error you’re actually getting.

1 like
Shivamyadav's avatar

@martinbean thanks for your advice sir. I've solved the issue by by publishing the cors.php and adding the Accept property in the options header every thing is now working properly.

2 likes

Please or to participate in this conversation.