In cakephp 3 I got error Unexpected field in POST data. Actually that field is not in my table, but I want to use in controller.
In cakephp 3 I got error Unexpected field in POST data
6.4k Views Asked by Masud At
2
There are 2 best solutions below
0
On
I was getting the similar error in cakephp 3.4 I was using the simple html form and input fields. I was passing the input fields data in array. like below:-
<form action="" method="post">
<input name="data[1][category_1]" id="category_1">
</form>
Then i do some R&D and found that we need to use the cakephp form helper to create the form and its fields like below :-
In case of pass form data in array
<?= $this->Form->create($user, ['url' => ['controller' => 'Users', 'action' => 'saveOrder']]); ?>
<?= $this->Form->input("Data.1.category_1"); ?>
<?= $this->Form->end() ?>
In case of simple input fields you can do the code like below
<?= $this->Form->create($user, ['url' => ['controller' => 'Users', 'action' => 'saveOrder']]); ?>
<?= $this->Form->input("category"); ?>
<?= $this->Form->end() ?>
This work form me and resolve the error Unexpected field in POST data in cakephp 3.4
The Security Component in CakePHP is not forgiving. If you want to allow a field thru that should not go thru the Security Component hashing process, you need to use the
unlockedFieldmethod that comes with theFormHelperclass as such:If this does not work, you will need to provide us with the pertinent code