如何在控制器中获取Laravel formrequest错误?
我知道我可以在视图中获得@if($ errors->任何())
或类似的错误。但是,如果我想在控制器
中获取验证错误将其返回为JSON怎么办?
regissionRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class RegisterRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|string|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'password_confirmation' => 'required|string|min:6|same:password',
];
}
}
authcontroller.php寄存器功能
public function register(RegisterRequest $request)
{
$request->errors();
}
是否有等待进行类似的事情以获取验证错误消息? 我在文档中找不到任何东西
I know i can get the errors in a view with @if ($errors->any())
or similars. But what if I want to get the validation errors in the Controller
to return it as JSON?
RegisterRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class RegisterRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|string|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'password_confirmation' => 'required|string|min:6|same:password',
];
}
}
AuthController.php register function
public function register(RegisterRequest $request)
{
$request->errors();
}
Is there a wait to do something like that to get the validation error messages?
I couldn't find anything in the documentation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于返回
JSON
错误响应,您必须覆盖in
regissn> regissne> regissn Request
也不要忘记导入以下内容。
For returning
json
error response ,you have to overridefailedValidation
inRegisterRequest
also dont forget to import following
请求文件中,
在控制器函数中的
来自:
laravel中的form-request?
In the Request file
In the controller function
From:
How to check if validation fail when using form-request in Laravel?