問題描述
我想在表單中創建一個 Cakephp 刪除帖子鏈接,如下所示.但是當我在瀏覽器中檢查時,第一個刪除帖子按鈕不包括刪除表單并且無法刪除,但其他按鈕包括我想要的并且可以刪除.
I want to create a Cakephp delete post link within form like the following. But the very first delete post button doesn't include delete Form when I inspect in browser and can't delete but the others include as I want and can delete.
是 cakephp 錯誤還是我需要更改源代碼?
Is it cakephp bug or something I need to change my source code?
<?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();
?>
推薦答案
表單不能嵌套,HTML 標準根據定義禁止嵌套.如果您嘗試這樣做,大多數瀏覽器將刪除嵌套表單并將其內容呈現在父表單之外.
Forms cannot be nested, the HTML standard forbids that by definition. If you try to, most browsers will drop the nested form and render its contents outside of the parent form.
如果您需要在現有表單中發布鏈接,那么您必須使用 inline
或 block
選項(從 CakePHP 2.5 開始可用,inline
> 已在 CakePHP 3.x 中刪除),以便將新表單設置為可以在主表單之外呈現的視圖塊.
If you need post links inside of existing forms, then you must use the inline
or block
options (available as of CakePHP 2.5, inline
has been removed in CakePHP 3.x), so that the new form is being set to a view block that can be rendered outside of the main form.
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
- API > FormHelper::postLink()
- 食譜 > 視圖 > 使用視圖塊
CakePHP 3.x
- API > CakeViewHelperFormHelper::postLink()
- Cookbook > Views > Helpers > Form > 創建獨立按鈕和 POST 鏈接
- 食譜 > 視圖 > 使用視圖塊
這篇關于如何在表單內使用 FormHelper::postLink()?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!