2017-03-16 81 views
0

如何在返回的響應中顯示驗證錯誤和消息。 我只是檢索響應這樣的:在crud api中顯示驗證錯誤

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

回答

1

我發現它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']); 
    } 
}