php Cakephp 隐藏输入框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19213165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:00:53  来源:igfitidea点击:

Cakephp hidden input field

phpcakephp

提问by Marc Rasmussen

So i have this field i want to keep hidden in my form.

所以我有这个字段,我想隐藏在我的表单中。

For this purpose i have tried the following:

为此,我尝试了以下方法:

<?php echo $this->Form->input('group_id', array('hiddenField' => true, 'value'=> 2)); ?>

I also tried:

我也试过:

<?php echo $this->Form->input('group_id', array('options' => array('hiddenField'=> 'true'), 'value'=>2 )); ?>

How ever i still see the input field..

我怎么仍然看到输入字段..

What am i doing wrong?

我究竟做错了什么?

回答by mark

You misread the documentation, I assume. hiddenFieldis to enable/disable specific hidden fields for specific form fields.

你误读了文档,我想。 hiddenField是启用/禁用特定表单字段的特定隐藏字段。

You are either looking for

你要么正在寻找

$this->Form->hidden('group_id')

or

或者

$this->Form->input('group_id', ['type' => 'hidden']);

I usually only use the latter.

我通常只使用后者。

See http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

BUT - that said - you shouldnt actually use either one of those. And omit any fields that serve no real purpose for the view and its form. Instead you should inject those fields into the data array prior to saving. See http://www.dereuromark.de/2010/06/23/working-with-forms/

但是 - 也就是说 - 你实际上不应该使用其中任何一个。并省略对视图及其形式没有实际用途的任何字段。相反,您应该在保存之前将这些字段注入到数据数组中。见http://www.dereuromark.de/2010/06/23/working-with-forms/

回答by spitfire

If you are looking to add a hidden field that uses a related second data array that will not be passed via post or put by default, you can use this to pass it:

如果您要添加一个隐藏字段,该字段使用相关的第二个数据数组,默认情况下不会通过 post 或 put 传递,您可以使用它来传递它:

echo $this->Form->hidden('Group.name');

This is useful for echoing out edit page titles when the post or put encounters an error. A dynamic title can lose Group.namedata array when your form is set up such as this:

这对于在 post 或 put 遇到错误时回显编辑页面标题很有用。Group.name当您的表单设置如下时,动态标题可能会丢失数据数组:

<h1>Edit Group - <?php echo h($this->request->data['Group']['name']); ?></h1>

For data that is to be saved to db however, follow Mark's suggestion above.

但是,对于要保存到 db 的数据,请遵循上面 Mark 的建议。

回答by Dinesh Vaitage

Try following code in cakephp 3 to set hidden field

尝试在 cakephp 3 中使用以下代码来设置隐藏字段

<?php
        echo $this->Form->hidden('name');
 ?>