0

I did move from Lumen to Laravel and now converting my project over. Everything is working except the validation. For some reason, if I try to validate, it just redirects to the welcome.blade.php view. What could cause this?

I am using only the API part of routes, not the view. I am not dealing with views. I am using the stateless part of Laravel.

According to documentation, I can validate like this:

$this->validate($request, [
        'title' => 'required|unique|max:255',
        'body' => 'required',
]);

If validation passes, your code will keep executing normally. However, if validation fails, an Illuminate\Contracts\Validation\ValidationException will be thrown.

I also tried to force it to return JSON response without success:

$validator = $this->validate($request, ['email' => 'required']);

         if ($validator->fails()) {
            $messages = $validator->errors();
            return new JsonResponse(['status' => 'error', 'messages' => $messages]);
         }

However, mine doesn't fail but just returns the welcome view with response code of 200. I have tried pretty much all the possible validation methods from the documentation and from google. Non of them are working.

I even tried with clean laravel install, declared one test route and test controller which had the validation and the result is the exact same.

Is the validation even meant to be compatible with the restful/stateless part of Laravel?

Any suggestion is much appreciated.

1
  • can you post your code here? so we can debug it Commented Aug 10, 2017 at 16:08

2 Answers 2

0

1- first the unique key needs a table, per example if you want the email to be unique in the users table you do as follows:

'email' => 'required|unique:users',
Sign up to request clarification or add additional context in comments.

2 Comments

As I mentioned in the question - I am not dealing with the views. I am using the stateless form of Laravel.
Aha, what does the response contain?
0

I think may be you have placed your route in route/web.php file. Replace that code from web.php to api.php

Try to place your API endpoints in route/api.php file.

And remember you need to add prefix /api in your route.

Ex : test.com/api/users.

4 Comments

I already remover the prefix from the RouteServiceProvider.php because my api is subdomain. My routes stay inside routes/api.php. I know the differences.
Did you try to use route/api.php file ??
That is what I am using.
$this->validate($request, [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.