简体   繁体   English

Laravel 5.2-使用API​​进行验证

[英]Laravel 5.2 - Validation with API

I am using Laravel 5.2 for validation with a REST JSON API. 我正在使用Laravel 5.2通过REST JSON API进行验证。

I have a UserController that extends Controller and uses the ValidatesRequests trait. 我有一个UserController ,它扩展了Controller并使用ValidatesRequests特性。

Sample code: 样例代码:

$this->validate($request, [
    'email'         => 'required|email',
    'password'      => 'required|min:4|max:72',
    'identifier'    => 'required|min:4|max:36',
    'role'          => 'required|integer|exists:role,id',
]);

This throws an exception, so in my Exceptions/Handler.php I have this code: 这会引发异常,因此在我的Exceptions/Handler.php我有以下代码:

public function render($request, Exception $e)
{
   return response()->json([
       'responseCode'  => 1,
       'responseTxt'   => $e->getMessage(),
   ], 400);
}

However, when validating responseTxt is always: 但是,在验证responseTxt始终为:

Array
(
   [responseCode] => 1
   [responseTxt] => The given data failed to pass validation.
)

I have used Laravel 4.2 in the past and remember the validation errors providing more detail about what failed to validate. 过去我曾经使用过Laravel 4.2,并且记得验证错误提供了有关验证失败的更多详细信息。

How can I know which field failed validation and why? 我怎么知道哪个字段验证失败,为什么?

A ValidationException gets thrown which gets rendered as a single generic message if the request wants JSON. 如果请求需要JSON,则会抛出ValidationException异常,该异常将被呈现为单个通用消息。

Instead of using the validate() method, manually invoke the validator, eg: 代替使用validate()方法,手动调用验证器,例如:

$validator = Validator::make($request->all(), [
    'email'         => 'required|email',
    'password'      => 'required|min:4|max:72',
    'identifier'    => 'required|min:4|max:36',
    'role'          => 'required|integer|exists:role,id',
]);
if ($validator->fails()) {
    return new JsonResponse(['errors' => $validator->messages()], 422);
}

https://laravel.com/docs/5.2/validation#manually-creating-validators https://laravel.com/docs/5.2/validation#manually-creating-validators

As an aside, I'd recommend a 422 HTTP status code rather than a 400. A 400 usually implies malformed syntax. 顺便说一句,我建议您使用422 HTTP状态代码,而不是400。400通常表示语法格式错误。 Something that causes a validation error usually means that the syntax was correct, but the data was not valid. 导致验证错误的内容通常表示语法正确,但是数据无效。 422 means "Unprocessable Entity" https://tools.ietf.org/html/rfc4918#section-11.2 422表示“无法处理的实体” https://tools.ietf.org/html/rfc4918#section-11.2

In Laravel 5.2 validate() method throws a Illuminate\\Validation\\ValidationException, so if you want to get the response use getResponse() instead getMessage(). 在Laravel 5.2中validate()方法抛出Illuminate \\ Validation \\ ValidationException,因此,如果要获取响应,请使用getResponse()而不是getMessage()。

For example, an easy way to handle this exception could be doing something like this: 例如,处理此异常的一种简单方法是执行以下操作:

try{
   $this->validate($request, [
       'email'         => 'required|email',
       'password'      => 'required|min:4|max:72',
       'identifier'    => 'required|min:4|max:36',
       'role'          => 'required|integer|exists:role,id'
   ]);
}catch( \Illuminate\Validation\ValidationException $e ){
    return $e->getResponse();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM