Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自定义验证错误格式laravel 5.2_Php_Json_Rest_Laravel_Laravel 5.2 - Fatal编程技术网

Php 自定义验证错误格式laravel 5.2

Php 自定义验证错误格式laravel 5.2,php,json,rest,laravel,laravel-5.2,Php,Json,Rest,Laravel,Laravel 5.2,我需要API的自定义验证json响应。现在默认的错误消息如下 {"password":["The password must be at least 6 characters."],"type":["The type field is required."]} 我想要 {flag:0,msg:"The password must be at least 6 characters.",data:{"password":["The password must be at least 6 chara

我需要API的自定义验证json响应。现在默认的错误消息如下

{"password":["The password must be at least 6 characters."],"type":["The type field is required."]}
我想要

{flag:0,msg:"The password must be at least 6 characters.",data:{"password":["The password must be at least 6 characters."],"type":["The type field is required."]}}

我只希望我的RESTAPI使用这种格式。如何实现它。

验证触发了我们可以捕获并执行的HttpResponseException

在控制器操作中执行以下操作

public function abc(Request $request)
{
    //using try catch to catch the validation exception thrown
    try{
       $this->validate($request, $rules);
    }catch(\Illuminate\Http\Exception\HttpResponseException $e){

         //fetching the error messages 
         $messages = $e->getResponse()->getSession()->get('errors')->getMessages();

         //or you can use $messages = session()->get('errors')->getMessages();

         $messages = collect($messages);
         $content = [
             'flag' => 0,
             'data' => $messages->toArray(),
             'msg' => $messages->first()[0]
         ];

         return response()->json($content, 400);
    }

}

验证触发我们可以捕获并执行的HttpResponseException

在控制器操作中执行以下操作

public function abc(Request $request)
{
    //using try catch to catch the validation exception thrown
    try{
       $this->validate($request, $rules);
    }catch(\Illuminate\Http\Exception\HttpResponseException $e){

         //fetching the error messages 
         $messages = $e->getResponse()->getSession()->get('errors')->getMessages();

         //or you can use $messages = session()->get('errors')->getMessages();

         $messages = collect($messages);
         $content = [
             'flag' => 0,
             'data' => $messages->toArray(),
             'msg' => $messages->first()[0]
         ];

         return response()->json($content, 400);
    }

}
我找到了解决办法

我更改了父控制器并添加了formatValidationErrors方法。给你

namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Illuminate\Contracts\Validation\Validator;
class Controller extends BaseController
{
    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;

    protected function formatValidationErrors(Validator $validator)
    {
        $res = $validator->errors()->all();
        if(\Request::wantsJson())
        {
            $res = ["flag" => 0,
                    "msg" => $validator->errors()->first()];
            $res['data'] = $validator->errors()->all();
        }
        return $res;
    }
}
我找到了解决办法

我更改了父控制器并添加了formatValidationErrors方法。给你

namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Illuminate\Contracts\Validation\Validator;
class Controller extends BaseController
{
    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;

    protected function formatValidationErrors(Validator $validator)
    {
        $res = $validator->errors()->all();
        if(\Request::wantsJson())
        {
            $res = ["flag" => 0,
                    "msg" => $validator->errors()->first()];
            $res['data'] = $validator->errors()->all();
        }
        return $res;
    }
}

您使用的是FormRequest还是simple Validator?我使用Illumb\Http\Request,然后使用$this->validate($this->Request,$rules);简言之,我想如上所述自定义MessageBag对象。您使用的是FormRequest还是简单验证器?我使用Illumb\Http\Request,然后使用$this->validate($this->Request,$rules);简言之,我想如上所述自定义MessageBag对象。这将起作用,但我必须在API的每个新服务上添加此代码,或者我可以在
Exceptions\Handler.php
中处理此异常。我找到了替代解决方案,请检查我的Answare。这将起作用,但我必须在我的API的每个新服务上添加此代码,或者我可以在
Exceptions\Handler.php
中处理此异常。我找到了替代解决方案,请检查我的答案。对于L5.5,请检查此URL:对于L5.5,请检查此URL: