如何在返回的响应中显示验证错误和消息。 我只是检索这样的回复:
{
"message": "4 validation errors occurred",
"url": "/api/posts",
"code": 422
}
答案 0 :(得分:2)
I found it here:
The Exception handler need a configuration:
-If your CakePHP >= 3.3 (middleware feature):
'Error' => [
'errorLevel' => E_ALL,
'exceptionRenderer' => 'Crud\Error\JsonApiExceptionRenderer',
'skipLog' => [],
'log' => true,
'trace' => true,
],
-If your CakePHP < 3.3 :
<?php
class AppController extends Controller {
public function initialize()
{
parent::initialize();
$this->Crud->config(['listeners.api.exceptionRenderer' => 'App\Error\ExceptionRenderer']);
}
}