如何在表单中使用FormHelper :: postLink()? [英] How to use FormHelper::postLink() inside of a form?

查看:93
本文介绍了如何在表单中使用FormHelper :: postLink()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在窗体中创建一个Cakephp删除帖子链接,如下所示。但是,当我在浏览器中检查时,第一个删除帖子按钮不包括删除表单,但不能删除,但其他人包括我想要并可以删除。



它是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'=> __('您确定要删除')));
endforeach;

echo $ this-> Form-> button('Submit',array('class'=>'btn btn- success pull-right'));
echo $ this-> Form-> end();
?>


解决方案

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



如果您需要在现有表单中添加帖子链接,使用 inline 选项(从CakePHP 2.5, inline 已在CakePHP 3.x中删除,因此新表单被设置为可以在主窗体之外渲染的视图块。



CakePHP 2.x

  echo $ this-> Form-> postLink b'Delete',
array(
'action'=>'delete',
$ attendanceID
) => false,//在那里,禁用inline rendering
'class'=>'btn btn-dark btn-sm col-md-4',
'confirm'=> __ ('您确定要删除')

);

CakePHP 3.x

  echo $ this-> Form-> postLink(
'Delete',
[
'action'=> delete',
$ attendanceid
],
[
'block'=> true,//禁用内嵌表单创建
'class'=>'btn bbn-dark btn-sm col-md-4',
'confirm'=> __('您确定要删除')
]
);

关闭主表单并输出帖子链接表单

  // ... 

echo $ this-> Form->结束();

// ...

echo $ this-> fetch('postLink'); //在主窗体之外输出帖子链接表单



CakePHP 2.x





CakePHP 3.x




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.

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

解决方案

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.

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')
    ]
);

Close the main form and output post link forms

// ...

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

// ...

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

See also

CakePHP 2.x

CakePHP 3.x

这篇关于如何在表单中使用FormHelper :: postLink()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆