2016-01-26 20 views
3

我想創建一個Cakephp刪除帖子鏈接,如下所示。但是,在瀏覽器中檢查的時候,最初的刪除發佈按鈕不包括刪除形式,不能刪除,但其他的包含因爲我想要刪除。如何在表單中使用FormHelper :: postLink()?

它是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(); 
?> 

回答

12

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

+0

你知道爲什麼使用這種方法時(將 '塊'=>真實postLinks)它改變了父窗體獲取請求? – Battousai

+0

@Battousai不,我不... – ndm

相關問題