The Add action (POST method) in CakePHP 3 is not working and it is asking fields that I already informed in json message. Let me explain:
What is my scenario:
- I'm using WAMP and my Apache is
mod_rewriteactivated; - I'm using composer and I installed CakePHP 3 and CRUD (friendsofcake/crud);
- I set MySQL database connection correctly;
- I'm using MySQL example database
sakila; - I generated
Countrymodel from Bake; - I changed the
AppControllerclass content to load CRUD plugins changing it's content to:
namespace App\Controller;
use Cake\Controller\Controller;
class AppController extends Controller {
use \Crud\Controller\ControllerTrait;
public $components = [
'RequestHandler',
'Crud.Crud' => [
'actions' => [
'Crud.Index',
'Crud.View',
'Crud.Add',
'Crud.Edit',
'Crud.Delete'
],
'listeners' => [
'Crud.Api',
'Crud.ApiPagination'
]
]
];
}
- I added the Router::extensions(['json', 'xml']) in
app/config/routes.php; - I added $routes->resources('Country') without deleting the default routes existing in
app/config/routes.php; - My root site address is http://localhost:8080/mtag
How problem happen:
I'm using Postman for Chrome and setting header Accept and Content-Type value to application/json and sending POST to following link: http://localhost:8080/mtag/country with the following json content:
{
"country": "BlaBlaBla",
"last_update": "2015-12-24"
}
But I receive the following status code: 412 Precondition Failed. In returned json:
success: false,
data: {
code: 412,
message: "2 validation errors occured",
}
The missing fields indicated is country and last_update but I informed them in json. What I did wrong? Did someone have an example how to use CakePHP CRUD correctly using json?