我正在使用Laravel 5.8和vue构建SPA;用于API身份验证的Laravel Passport。一切顺利,直到上周,当我发现验证失败时我停止获取422。我开始改为输入200,但验证失败,并返回通常的“ {”状态”:“失败”,“错误”:“给定的数据无效。”}”。即使用户在Laravel RegisterController上注册并验证失败,我也能获得这些。在此之前,Laravel将返回302并标识错误的字段。现在我得到的是一个空白页,其中包含{“ status”:“ failed”,“ error”:“给定的数据无效。”}。进入API本身,与表单验证相同。失败时返回200,而不是422。因此<has-error :form="form" field="category"></has-error>
已停止显示我的应用程序中的所有错误。真令人沮丧。我已经检查了我的代码一百万次,一切看起来都很完美。请记住,经过4个月的正确操作,这种情况突然在一周前恢复。
注册控制器
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Notifications\Notifiable;
use Notification;
use Auth;
use App\Mail\WelcomeMail;
use Mail;
use App\Notifications\newUser;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/dashboard';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'firstname' => ['required', 'string', 'max:255'],
'surname' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'mobile' => ['required', 'digits:11'],
'state' => ['required', 'string', 'max:50'],
'password' => ['required', 'string', 'min:8'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
$user = User::create([
'firstname' => $data['firstname'],
'surname' => $data['surname'],
'email' => $data['email'],
'mobile' => $data['mobile'],
'state' => $data['state'],
'role' => $data['role'],
'password' => Hash::make($data['password']),
'bonus' => 1000,
'company' => $data['company']
]);
$admin = User::where('role', 'admin')->get();
Notification::send($admin, new newUser($user));
Mail::to($data['email'])->send(new WelcomeMail($user));
return $user;
}
}
Register.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel='manifest' href='/manifest.webmanifest'>
<title>dafixas</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12 text-center signupTitle">
<a href="/"><img src="img/Megalabourers_logo.jpg"><span> dafixas</span></a>
</div>
<div class="row">
</div>
<div class="col-md-8">
<div class="card card-margin">
<div class="card-body">
<p>You're Amazing! Let's Get to Know You.</p>
<form method="POST" action="{{ route('register') }}">
@csrf
<div class="form-group row">
<label for="firstname"></label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control @error('firstname') is-invalid @enderror" name="firstname" value="{{ old('firstname') }}" required autocomplete="firstname" autofocus placeholder="First Name">
@error('firstname')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<label for="surname"></label>
<div class="col-md-6">
<input id="surname" type="text" class="form-control @error('surname') is-invalid @enderror" name="surname" value="{{ old('surname') }}" required autocomplete="surname" placeholder="Surname">
@error('surname')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email"></label>
<div class="col-md-12">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" placeholder="E-Mail">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="mobile"></label>
<div class="col-md-12">
<input id="mobile" type="text" class="form-control @error('mobile') is-invalid @enderror" name="mobile" value="{{ old('mobile') }}" required autocomplete="mobile" placeholder="Mobile Number">
@error('mobile')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="company"></label>
<div class="col-md-12">
<input id="company" type="text" class="form-control @error('mobile') is-invalid @enderror" name="company" value="{{ old('company') }}" autocomplete="company" placeholder="Company Name">
@error('company')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="city"></label>
<div class="col-md-12">
<select class="form-control" id="state" name="state" class="form-control">
<option selected="true" disabled="disabled">Please select Your State of Residence</option>
<option value="Abia">Abia</option>
<option value="Adamawa">Adamawa</option>
<option value="Akwa Ibom">Akwa Ibom</option>
<option value="Anambra">Anambra</option>
<option value="Bauchi">Bauchi</option>
<option value="Bayelsa">Bayelsa</option>
<option value="Benue">Benue</option>
<option value="Borno">Borno</option>
<option value="Cross Rive">Cross River</option>
<option value="Delta">Delta</option>
<option value="Ebonyi">Ebonyi</option>
<option value="Edo">Edo</option>
<option value="Ekiti">Ekiti</option>
<option value="Enugu">Enugu</option>
<option value="FCT">Federal Capital Territory</option>
<option value="Gombe">Gombe</option>
<option value="Imo">Imo</option>
<option value="Jigawa">Jigawa</option>
<option value="Kaduna">Kaduna</option>
<option value="Kano">Kano</option>
<option value="Katsina">Katsina</option>
<option value="Kebbi">Kebbi</option>
<option value="Kogi">Kogi</option>
<option value="Kwara">Kwara</option>
<option value="Lagos">Lagos</option>
<option value="Nasarawa">Nasarawa</option>
<option value="Niger">Niger</option>
<option value="Ogun">Ogun</option>
<option value="Ondo">Ondo</option>
<option value="Osun">Osun</option>
<option value="Oyo">Oyo</option>
<option value="Plateau">Plateau</option>
<option value="Rivers">Rivers</option>
<option value="Sokoto">Sokoto</option>
<option value="Taraba">Taraba</option>
<option value="Yobe">Yobe</option>
<option value="Zamfara">Zamfara</option>
</select>
@error('state')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password"></label>
<div class="col-md-12">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password" placeholder="Create Password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<input type="hidden" name="role" value="user">
<div class="form-group row mb-0">
<div class="col-md-12">
<button type="submit" class="btn btn-lg">
Create My Account
</button>
</div>
</div>
</form>
<div class="loginnow">
Already Have an Account? Please <a href="{{ route('login') }}">Log in</a>
</div>
<div class="loginnow">
<span>By clicking on Create My Account, you agree to our <a href="/terms">terms & conditions</a> and <a href="/privacy">privacy policy</a></span>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
非常感谢!
答案 0 :(得分:0)
这对我有用:
if ($exception instanceof ValidationException && $request->expectsJson()) {
return response()->json(['message' => 'The given data was invalid.', 'errors' => $exception->validator->getMessageBag()], 422);
}
在app \ Exceptions \ Handler.php中,呈现功能。但是我仍然无法理解最初出了什么问题。