在crud api中显示验证错误

时间:2017-03-16 22:35:08

标签: api validation debugging cakephp-3.0 crud

如何在返回的响应中显示验证错误和消息。 我只是检索这样的回复:

{
  "message": "4 validation errors occurred",
  "url": "/api/posts",
  "code": 422
} 

1 个答案:

答案 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']);
  }
}