gpt4 book ai didi

Cakephp表单-有没有办法使字段只读(在 View 中)

转载 作者:行者123 更新时间:2023-12-03 15:04:09 26 4
gpt4 key购买 nike

我有一个Cakephp 1.3表单,该表单允许用户编辑配置文件数据。但是,表格中的某些信息需要是只读的(有时)。

我是在只读情况下回显和格式化字段内容的唯一选择,还是Cake表单中是否存在允许只读字段的标志。理想情况下,类似于其他界面,只读字段将显示为灰色。

    echo $this->Form->create('User', array('url' => array('controller' => 'User', 'action'=>'editUser')));

echo $this->Form->input('id', array('type'=>'hidden'));

If (!isset($IsAdmin)) {
// Only display username - read only! Add code here
echo $this->Form->input('username', array('label' => __d('users', 'User',true)));
} else {
// Admins can edit user names
echo $this->Form->input('username', array('label' => __d('users', 'User',true)));
}

... more fields here

echo $this->Form->end(__d('users', 'Submit',true));

最佳答案

您可以在options数组中添加“disabled”键,但是要意识到这只是表单的前端/表示,人们将能够覆盖输入字段的“disabled”属性并修改其值。

为了防止不必要的更改被保存,使用模型保存数据时需要指定“fieldList”

输出禁用的表单字段;

echo $this->Form->input('fieldname', array('type'=>'hidden', 'disabled' => 'disabled'));

然后,在保存数据时,指定一个字段列表(文档: http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html#saving-your-data)
$this->MyModel->save($this->data, true, array('field1', 'field2'));

字段列表应包括用户允许更新的所有字段

关于Cakephp表单-有没有办法使字段只读(在 View 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15461501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com