Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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抛出一个自定义ValidationException_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel抛出一个自定义ValidationException

Php Laravel抛出一个自定义ValidationException,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在尝试抛出一个自定义异常。该项目是一个使用Google Places API的API,如果结果的状态为ZERO RESULTS,我需要抛出一个验证异常。这是因为存储的数据是错误的 更清楚地了解这一点。注册用户修改其个人资料,包括地址、邮政编码等。 然后,我们有一个get方法,在该方法中,我们对places API进行查询,以获取纬度和经度,并将其添加到概要文件模型中 if (strcmp($status, 'ZERO_RESULTS') == 0 || strcmp($status, 'IN

我正在尝试抛出一个自定义异常。该项目是一个使用Google Places API的API,如果结果的状态为
ZERO RESULTS
,我需要抛出一个验证异常。这是因为存储的数据是错误的

更清楚地了解这一点。注册用户修改其个人资料,包括地址、邮政编码等。 然后,我们有一个get方法,在该方法中,我们对places API进行查询,以获取纬度和经度,并将其添加到概要文件模型中

if (strcmp($status, 'ZERO_RESULTS') == 0 || strcmp($status, 'INVALID_REQUEST') == 0) {
    $error = ValidationException::withMessages([
        "one_thing" => ["Validation Message #1"], "another_thing" => ['Validation Message #2']
    ]);
    throw $error;
}
我读了一些关于StackOverflow的答案,我甚至尝试了这些答案,但我只得到以下错误:

{
  "errors": [
    {
      "status": 500,
      "code": 1,
      "source": {
        "pointer": "ErrorException line 73 in /Users/jacobotapia/Documents/Espora/one-mind-backend/vendor/sfelix-martins/json-exception-handler/src/ValidationHandler.php"
      },
      "title": "errorexception",
      "detail": "Undefined index: one_thing"
    }
  ]
}
我还想指出,所有过程都发生在
GET
方法中

我只想返回一个错误,说我们无法从GooglePlacesAPI获得任何结果。这样做的目的是告诉客户端,用户在应用程序中注册的配置文件数据是错误的


我做错了什么?

这是你问题的解决方案

解决方案1:使用http代码发送错误消息

return response()->json(["error"=> "YOUR_MESSAGE_HERE"],401);

这是您的问题的解决方案

解决方案1:使用http代码发送错误消息

return response()->json(["error"=> "YOUR_MESSAGE_HERE"],401);

如果您有字段名“one\u thing”和“one\u thing”,您可能想试试

检查withMessages()方法

这些键被视为字段名称,因此您将通过的$errors将与相应的字段相关

所以基本上就像

$error = \Illuminate\Validation\ValidationException::withMessages([
   'field_name_1' => ['Validation Message for field name 1'],
   'field_name_2' => ['Validation Message for field name 2'],
   'field_name_3' => ['Validation Message for field name 3'],
]);
throw $error;
试试这个

表格代码为

<form action="{{ route('test-validation') }}" method="POST">
    @csrf
    <input type="text" name="test" value="" />
    @if( $errors->has('test') ) 
        @foreach( $errors->get('test') as $err ) 
            {{ $err  }} 
        @endforeach 
    @endif
    <input type="submit" name="submit" value="Submit" />
 </form>

使用此代码,您应该能够正确地获取错误。

如果您有字段名“one\u thing”和“one\u thing”,您可能想尝试一下

检查withMessages()方法

这些键被视为字段名称,因此您将通过的$errors将与相应的字段相关

所以基本上就像

$error = \Illuminate\Validation\ValidationException::withMessages([
   'field_name_1' => ['Validation Message for field name 1'],
   'field_name_2' => ['Validation Message for field name 2'],
   'field_name_3' => ['Validation Message for field name 3'],
]);
throw $error;
试试这个

表格代码为

<form action="{{ route('test-validation') }}" method="POST">
    @csrf
    <input type="text" name="test" value="" />
    @if( $errors->has('test') ) 
        @foreach( $errors->get('test') as $err ) 
            {{ $err  }} 
        @endforeach 
    @endif
    <input type="submit" name="submit" value="Submit" />
 </form>


使用此代码,您应该能够正确地获得错误。

您想要的实际响应是什么?我们不明白,你想正确验证状态代码吗?我更新了问题,希望这能澄清问题doubt@JacoboTapia有字段名“one_thing”和“one_thing”吗?没有,这是一个GET方法。该请求中有任何参数,其中
withMessages
方法中的
应该是字段名称。您需要什么实际响应?我们不明白,你想正确验证状态代码吗?我更新了问题,希望这能澄清问题doubt@JacoboTapia有字段名“one_thing”和“one_thing”吗?没有,这是一个GET方法。该请求中有任何参数该
中的
和消息
方法应为字段名称。这不起作用:
“title”:“badmethodcallexception”,“detail”:“method Illumb\\Http\\JsonResponse::toArray不存在。”
您使用哪一版本的laravel?使用laravel以及您的php版本更新您的问题。这不起作用:
“title”:“badmethodcallexception”,“detail”:“Method Illumb\\Http\\JsonResponse::toArray不存在。”
您使用的是哪个版本的laravel?请使用laravel和php版本更新您的问题。现在,我在POST请求中有类似的字段,但它没有work@JacoboTapia我已经用示例代码更新了我的答案,请检查它是否能让您对它有更好的了解。现在我在POST请求中有类似的字段,但它没有work@JacoboTapia我已经用示例代码更新了我的答案,请检查它是否能让您更好地了解它。