在 crud 中显示验证错误 api
Show validation errors in crud api
如何在返回的响应中显示验证错误和消息。
我只是检索这样的响应:
{
"message": "4 validation errors occurred",
"url": "/api/posts",
"code": 422
}
找到了here:
异常处理程序需要配置:
-如果您的 CakePHP >= 3.3(中间件功能):
'Error' => [
'errorLevel' => E_ALL,
'exceptionRenderer' => 'Crud\Error\JsonApiExceptionRenderer',
'skipLog' => [],
'log' => true,
'trace' => true,
],
-如果您的 CakePHP < 3.3:
<?php
class AppController extends Controller {
public function initialize()
{
parent::initialize();
$this->Crud->config(['listeners.api.exceptionRenderer' => 'App\Error\ExceptionRenderer']);
}
}
如何在返回的响应中显示验证错误和消息。 我只是检索这样的响应:
{
"message": "4 validation errors occurred",
"url": "/api/posts",
"code": 422
}
找到了here:
异常处理程序需要配置:
-如果您的 CakePHP >= 3.3(中间件功能):
'Error' => [
'errorLevel' => E_ALL,
'exceptionRenderer' => 'Crud\Error\JsonApiExceptionRenderer',
'skipLog' => [],
'log' => true,
'trace' => true,
],
-如果您的 CakePHP < 3.3:
<?php
class AppController extends Controller {
public function initialize()
{
parent::initialize();
$this->Crud->config(['listeners.api.exceptionRenderer' => 'App\Error\ExceptionRenderer']);
}
}