A
A
Alexander Sharomet2018-07-23 16:15:36
Laravel
Alexander Sharomet, 2018-07-23 16:15:36

Why is laravel validator not returning a message?

Hello.
Why might the validator not show the message?
When you enter invalid data, they do not pass, but there is no standard message.
AuthController

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;

class AuthController extends Controller
{
    public function login(Request $request) 
    {
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required'
        ]); // Если данные не валидны то должно выдать стандартное сообщение, но ничего не происходит

        return response()->json([
            'token' => $token
        ], 200);
    }
}

api.php Router: Why is this happening? Thanks
Route::post('/login', '[email protected]');

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dkoro, 2018-07-23
@sharomet

So try:

$validator = Validator::make($request->all(), [
           'email' => 'required|email',
           'password' => 'required'
]);

if ($validator->fails()) {
  $errors = $validator->errors()->all();
  return response()->json([
    'error' => current($errors),
  ]);
}

M
mShpakov, 2018-07-23
@mShpakov

Try like this

$request->validate([
            'email' => 'required|email',
            'password' => 'required'
        ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question