In cakephp 3 I got error Unexpected field in POST data

6.4k views Asked by At

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.

2

There are 2 answers

0
kantsverma 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

1
AKKAweb On

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 unlockedField method that comes with the FormHelper class as such:

$this->Form->unlockField('field');

If this does not work, you will need to provide us with the pertinent code