Laravel请求使用自定义验证类触发两次 [英] Laravel request firing twice with custom validation class
问题描述
我的Laravel应用程序存在一个怪异的问题,一旦验证规则生效,该代码就会被调用两次.我已经将验证逻辑抽象到一个单独的类中,但是无论我如何使用该API(都使用Postman进行了尝试,使用jQuery),它看起来仍然运行两次,其输出看起来像这样:
I have a weird problem with my Laravel application, whereby this code gets called twice once the validation rules kick in. I have abstracted validation logic into a separate class, but no matter how I consume the API (tried using Postman, and with jQuery) it still appears to run twice with the output looking like this:
called{"email":["The email has already been taken."],"country":["The country must be a number."]}called{"email":["The email has already been taken."],"country":["The country must be a number."]}
我只期望一个JSON响应.我正在拔头发,尝试了两个不同的连接,但似乎无法弄清为什么自定义请求被调用两次.这是一个新的Laravel应用,因此没有太多代码与之冲突.
I am only expecting one JSON response. I'm tearing my hair out, tried on two different connections and can't seem to work out why the custom request is called twice. This is a new Laravel app, so there isn't much code to conflict with it.
//Create User Request extends standard request. Handles Validation
public function __construct(CreateUserRequest $request){
$this->request = $request;
}
public function register()
{
try{
$array = DB::transaction(function(){
$email = $this->request->input('email');
$password = $this->request->input('password');
$companyName = $this->request->input('companyName');
$userName = $this->request->input('name');
$country = $this->request->input('country');
$company = Company::create([
'name' => $companyName,
'active'=>true,
'country_id'=>$country
]);
$user = User::create([
'company_id' => $company->id,
'name'=>'admin',
'email' => $email,
'password' => $password,
'active' =>true
]);
if( !$company || !$user )
{
throw new \Exception('User not created for account');
}
return compact('company', 'user');
});
$token = JWTAuth::fromUser($array['user']);
return Response::json(compact('token'));
}
catch( Exception $e )
{
return Response::json(['error' => $e->getMessage() ], HttpResponse::HTTP_CONFLICT );
}
}
然后验证自定义请求.
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Response;
class CreateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
public function response(array $errors)
{
// return Response::json(['errorg' => $errors ], 200 );
echo('called');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|unique:users',
'password' => 'required',
'companyName' => 'required',
'name' => 'required',
'country' => 'required|numeric'
];
}
}
推荐答案
有趣.
尝试从__construct()
中删除CreateUserRequest $request
参数并将其添加到您的register()
方法中,如下所示:register(CreateUserRequest $request)
.并通过调用$request
而不是$this->request
来使用您的请求.
Try to remove CreateUserRequest $request
parameter from __construct()
and add it to your register()
method like this: register(CreateUserRequest $request)
. And use your request by calling $request
instead of $this->request
.
这篇关于Laravel请求使用自定义验证类触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!