如何在表单中使用FormHelper :: postLink()?

时间:2016-01-26 04:53:18

标签: php forms cakephp

我想在表单中创建一个Cakephp删除帖子链接,如下所示。但是当我在浏览器中检查时,第一个删除帖子按钮不包括删除表单,但是不能删除,但其他包括我想要的并且可以删除。

是cakephp bug还是我需要更改源代码的东西?

<?php
echo $this->Form->create('Attendance', array('required' => false, 'novalidate' => true));

foreach($i = 0; $i < 10; i++):
    echo $this->Form->input('someinput1', value => 'fromdb');
    echo $this->Form->input('someinput2', value => 'fromdb');
    echo $this->Form->postLink('Delete',array('action'=>'delete',$attendanceid),array('class' => 'btn btn-dark btn-sm col-md-4','confirm' => __('Are you sure you want to delete')));
endforeach;

echo $this->Form->button('Submit', array('class' => 'btn btn-success pull-right'));
echo $this->Form->end();
?>

1 个答案:

答案 0 :(得分:15)

Forms cannot be nested,HTML标准禁止根据定义。如果您尝试,大多数浏览器将删除嵌套表单并将其内容呈现在父表单之外。

如果您需要现有表单内的帖子链接,那么您必须使用inlineblock选项(CakePHP 2.5提供,inline已在CakePHP 3.x中删除),以便将新表单设置为可以在主表单之外呈现的视图块。

CakePHP 2.x

echo $this->Form->postLink(
    'Delete',
    array(
        'action' => 'delete',
        $attendanceid
    ),
    array(
        'inline' => false, // there you go, disable inline rendering
        'class' => 'btn btn-dark btn-sm col-md-4',
        'confirm' => __('Are you sure you want to delete')
    )
);

CakePHP 3.x

echo $this->Form->postLink(
    'Delete',
    [
        'action' => 'delete',
        $attendanceid
    ],
    [
        'block' => true, // disable inline form creation
        'class' => 'btn btn-dark btn-sm col-md-4',
        'confirm' => __('Are you sure you want to delete')
    ]
);

关闭主表单并输出帖子链接表单

// ...

echo $this->Form->end();

// ...

echo $this->fetch('postLink'); // output the post link form(s) outside of the main form

另见

CakePHP 2.x

CakePHP 3.x