隐藏文本输入而不使其成为 `type="hidden"`
Hiding a text input without making it `type="hidden"`
我有什么
我在提交时使用 FormController
到 create inputs. A specific input will be frequently updated by Javascript for internal purposes and I want it hidden. In this case, I can't use type="hidden"
, instead it needs to be type="text"
so that it won't be checked by the form tampering prevention。
我试过的
- 如果需要隐藏整个表单,则将
'hidden' => true
添加到 $this->Form->create()
选项即可。不过,该确切属性似乎不适用于使用 $this->Form->input()
和 $this->Form->control()
创建的单个输入
- 对于特定的输入,首先想到的是将
'style' => 'display:none'
添加到它的选项中,但这似乎不是一个干净的 CakePHP 方式解决方案
- 我想我已经看到有人在这里提到了一种方法来做到这一点。我认为这是您要添加到选项中的属性。我使用本网站和 Google 搜索了我的答案和评论,但一无所获。
请指教!
您可以创建隐藏输入并在需要时使其免于表单安全,方法是通过 unlockField()
方法解锁该字段:
$this->Form->unlockField('field_name');
echo $this->Form->hidden('field_name');
或通过为 secure
选项传递 false
或 'skip'
:
echo $this->Form->hidden('field_name', ['secure' => false]);
echo $this->Form->hidden('field_name', [
'secure' => \Cake\View\Helper\FormHelper::SECURE_SKIP
]);
另见
我有什么
我在提交时使用 FormController
到 create inputs. A specific input will be frequently updated by Javascript for internal purposes and I want it hidden. In this case, I can't use type="hidden"
, instead it needs to be type="text"
so that it won't be checked by the form tampering prevention。
我试过的
- 如果需要隐藏整个表单,则将
'hidden' => true
添加到$this->Form->create()
选项即可。不过,该确切属性似乎不适用于使用$this->Form->input()
和$this->Form->control()
创建的单个输入
- 对于特定的输入,首先想到的是将
'style' => 'display:none'
添加到它的选项中,但这似乎不是一个干净的 CakePHP 方式解决方案 - 我想我已经看到有人在这里提到了一种方法来做到这一点。我认为这是您要添加到选项中的属性。我使用本网站和 Google 搜索了我的答案和评论,但一无所获。
请指教!
您可以创建隐藏输入并在需要时使其免于表单安全,方法是通过 unlockField()
方法解锁该字段:
$this->Form->unlockField('field_name');
echo $this->Form->hidden('field_name');
或通过为 secure
选项传递 false
或 'skip'
:
echo $this->Form->hidden('field_name', ['secure' => false]);
echo $this->Form->hidden('field_name', [
'secure' => \Cake\View\Helper\FormHelper::SECURE_SKIP
]);
另见