2016-01-26 21 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

相关问题