How to use FormHelper::postLink() inside of a form?(如何在表单内使用 FormHelper::postLink()?)
问题描述
我想在表单中创建一个 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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在表单内使用 FormHelper::postLink()?


- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP - if 语句中的倒序 2021-01-01