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

Thu*_*ung 4 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();
?>
Run Code Online (Sandbox Code Playgroud)

ndm*_*ndm 16

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

如果您需要在现有表单内部发布链接,那么您必须使用inline或者block选项(从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')
    )
);
Run Code Online (Sandbox Code Playgroud)

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')
    ]
);
Run Code Online (Sandbox Code Playgroud)

关闭主窗体并输出post链接表单

// ...

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

// ...

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

也可以看看

CakePHP 2.x

CakePHP 3.x