无法在Cakephp中提交带有隐藏输入的表单

时间:2018-02-27 14:18:33

标签: php cakephp-3.0

所以我有一个包含隐藏输入的表单。

 <?= $this->Form->create(null, [ 'class' => '', 'templates' => 'Inspinia.form_basic']) ?>
    <?php
        echo $this->Form->control('name');
        echo $this->Form->control('description', ['type' => 'text']);
        echo $this->Form->control('chart_type', [ 'options' => $this->App->availableCharts() ] );
        echo $this->Form->control('frequency', [ 'options' => ['monthly' => 'Monthly','quarterly'=>'Quarterly','snapshot' =>'Snapshot','monthly/quarterly' => 'Monthly/Quarterly'] ] );
        echo $this->Form->control('public', [ 'options' => ['1' => 'Public','0' => 'Private'] ] );
        // $this->Form->unlockField('deleted');
        echo $this->Form->hidden('deleted',['value' => 0]);
    ?>
<?= $this->Form->button(__('Save'), ['class' => 'btn btn-sm btn-primary pull-right m-t-n-xs']) ?>
<?= $this->Form->end() ?>

每当我尝试提交表单时,都会抛出此错误

Missing field 'deleted' in POST data

我知道我可以通过

来绕过这个
$this->Form->unlockField('deleted');

但是我不想绕过Cakephp中的安全组件,所以有没有其他方法可以让CakePhp允许我提交这个隐藏字段?

这是我的控制器没什么太多但是这里以防万一你们想知道

public function test() {

    if ($this->request->is('post')) {
        debug($this->request->data);
    }
}   

1 个答案:

答案 0 :(得分:1)

它应该在下面

<?php
  echo $this->Form->input('nameoffield',array('type'=>'hidden')); 
?>

或传递隐藏值

<?php
$hidden_value = 0;
echo $this->Form->input('nameoffield',array('type'=>'hidden','value' => $hidden_value)); 
?>