2017-09-16 35 views
0

我没有得到我期望的响应。失败的验证返回默认错误消息,即使提供了自定义消息

这是一个位置的Web服务请求的控制器代码:

<?php 
namespace App\Http\Controllers; 

use App\Location; 
use Illuminate\Http\Request; 


class LocationController extends Controller 
{ 


    /** 
    * Action method to add a location with the supplied Data 
    * 
    * @param \Illuminate\Http\Request $p_oRequest Request 
    * 
    * @return JSON 
    */ 
    public function add(Request $p_oRequest) 
    { 

     try { 

      $p_oRequest->validate(
       array(
        'name' => 'required|alpha_num', 
        'user_id' => 'required|integer', 
       ), 
       array(
        'name.required' => 'Name is required', 
        'name.string' => 'Name must be alphanumeric', 
        'user_id.required' => 'Curator User Id is required', 
        'user_id.required' => 'Curator User Id must be an integer', 
       ) 
      ); 

     } catch (\Exception $ex) { 

      $arrResponse = array(
       'result' => 0, 
       'reason' => $ex->getMessage(), 
       'data' => array(), 
       'statusCode' => 404 
      ); 

     } finally { 

      return response()->json($arrResponse); 

     } 

    } 

} 

请求是http://mydomain/index.php/api/v1/location/[email protected]^

响应的原因,我想到的是:{ “结果”:0,“原因“:”Name must be alphanumeric“,”data“:[],”statusCode“:404}

我得到的实际响应是:{”result“:0,”reason“:”给定的数据是无效“,”data“:[],”statusCode“:404}

请帮忙。这正在扰乱我。

回答

0

你的消息应该是验证规则,所以不是:

'name.required' => 'Name is required', 
'name.string' => 'Name must be alphanumeric', 
'user_id.required' => 'Curator User Id is required', 
'user_id.required' => 'Curator User Id must be an integer', 

你应该有:

'name.required' => 'Name is required', 
'name.alpha_num' => 'Name must be alphanumeric', 
'user_id.required' => 'Curator User Id is required', 
'user_id.integer' => 'Curator User Id must be an integer', 
+0

这仍然没有解决问题。显然这是其他Laravel开发人员遇到的问题。看看https://github.com/laravel/framework/issues/21059 – user3125602

+0

不,你设置了无效的消息。当你使用'alpha_num'验证规则时,你应该使用'name.alpha_num'来使它工作。所以要么你做错了什么,要么你在你的问题中输入了错误的代码 –

+0

感谢你的回应,Marcin。我做了你推荐的更正。所有功劳都归功于你,你是对的 - 我犯了错误,他们会创造他们自己的问题,但是,我的JSON中无用的响应的根本原因仍然没有解决。我已经发现了更广泛的解决方案,但是我将作为答案发布。 – user3125602

0

,我终于发现了为什么这是行不通的。这不是实施代码或Laravel中的错误问题,而是以下两者之一:(i)。编写好的PHP代码来处理不言自明的结果,这显然我没有做到; (ⅱ)。 Laravel内部有关如何实际使用验证错误响应的文档不足。拿你的选择。

Laravel的验证会抛出Illuminate \ Validation \ ValidationError。信不信由你,这实际上默认错误信息是“给定的数据是无效的”,所以当你捕获一个\ Exception并且检索它的$ e-> getMessage()时,这个默认的错误信息就是你(正确地)得到。

你需要做的是捕获\ Illuminate \ Validation \ ValidationError - 我原本应该做的,呃! - 然后使用它的方法来帮助您从中提取错误消息。

这里的解决方案,我想出来的:

<?php 
namespace App\Http\Controllers; 

use App\Location; 
use Illuminate\Http\Request; 


class LocationController extends Controller 
{ 

    /** 
    * Action method to add a location with the supplied Data 
    * 
    * @param \Illuminate\Http\Request $p_oRequest Request 
    * 
    * @return JSON 
    */ 
    public function add(Request $p_oRequest) 
    { 
     try { 

      $arrValid = array(
       'name' => 'required|alpha_num', 
       'user_id' => 'required|integer', 
      ); 
      $p_oRequest->validate(
       $arrValid, 
       array(
        'name.required' => 'Name is missing', 
        'name.alpha_num' => 'Name must be alphanumeric', 
        'user_id.required' => 'User Id is missing', 
        'user_id.integer' => 'User Id must be an integer', 
       ) 
      ); 

     } catch (\Illuminate\Validate\ValidationException $e) { 

      /** 
      * Validation failed 
      * Tell the end-user why 
      */ 
      $arrError = $e->errors(); // Useful method - thank you Laravel 
      /** 
      * Compile a string of error-messages 
      */ 
      foreach ($arrValid as $key=>$value) { 
       $arrImplode[] = implode(', ', $arrError[$key]); 
      } 
      $message = implode(', ', $arrImplode); 
      /** 
      * Populate the respose array for the JSON 
      */ 
      $arrResponse = array(
       'result' => 0, 
       'reason' => $message, 
       'data' => array(), 
       'statusCode' => $e->status, 
      ); 

     } catch (\Exception $ex) { 

      $arrResponse = array(
       'result' => 0, 
       'reason' => $ex->getMessage(), 
       'data' => array(), 
       'statusCode' => 404 
      ); 

     } finally { 

      return response()->json($arrResponse); 

     } 

    } 

} 

所以,的确,Laravel被提供正确的反应,并没有什么在锡的一边说,但我不适用它正确。无论如何,作为未来我和Laravel-sea的其他失去PHP的海员的帮助,我提供了解决方案。

此外,感谢Marcin指出我的错误编码,即使我已经实现了上述解决方案,这也会导致问题。