如何在控制器中获取Laravel formrequest错误?

发布于 2025-02-12 23:52:55 字数 817 浏览 3 评论 0原文

我知道我可以在视图中获得@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2025-02-19 23:52:55

对于返回JSON错误响应,您必须覆盖 in regissn> regissne> regissn Request

  protected function failedValidation(Validator $validator)
    {
       if ($this->ajax()){
        
           throw new HttpResponseException(response()->json($validator->errors(),419));
       } else{
           throw (new ValidationException($validator))
                        ->errorBag($this->errorBag)
                        ->redirectTo($this->getRedirectUrl());
       }
 }

也不要忘记导入以下内容。

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\ValidationException;

For returning json error response ,you have to override failedValidation in RegisterRequest

  protected function failedValidation(Validator $validator)
    {
       if ($this->ajax()){
        
           throw new HttpResponseException(response()->json($validator->errors(),419));
       } else{
           throw (new ValidationException($validator))
                        ->errorBag($this->errorBag)
                        ->redirectTo($this->getRedirectUrl());
       }
 }

also dont forget to import following

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\ValidationException;
笑梦风尘 2025-02-19 23:52:55

请求文件中,

public $validator = null;
protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
    $this->validator = $validator;
}

在控制器函数中的

if (isset($request->validator) && $request->validator->fails()) {
        $request->validator->errors()->messages()
    }

来自:
laravel中的form-request?

In the Request file

public $validator = null;
protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
    $this->validator = $validator;
}

In the controller function

if (isset($request->validator) && $request->validator->fails()) {
        $request->validator->errors()->messages()
    }

From:
How to check if validation fail when using form-request in Laravel?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文