我想在如下表单中创建一个 Cakephp 删除帖子链接。但是当我在浏览器中检查并且无法删除时,第一个删除帖子按钮不包括删除表单,但其他按钮包括我想要的并且可以删除。
是 cakephp 错误还是我需要更改源代码?
<?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();
?>
最佳答案
Forms cannot be nested ,HTML 标准根据定义禁止这样做。如果您尝试这样做,大多数浏览器将丢弃嵌套表单并将其内容呈现在父表单之外。
如果您需要在现有表单中发布链接,那么您必须使用 inline
或 block
选项(自 CakePHP 2.5 起可用,inline
已在 CakePHP 3.x 中删除),以便将新表单设置为可以在主表单之外呈现的 View block 。
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')
]
);
关闭主窗体并输出post链接窗体
// ...
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
关于php - 如何在表单中使用 FormHelper::postLink()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35007237/