UnlockField在数组的CakePHP中不起作用



我有一个表单,它必须将一些元素作为数组的一部分来处理。

echo $this->Form->control('config.sys_file_id', ['type' => 'number']);
echo $this->Form->control('click_enlarge', ['type' => 'checkbox']);
echo $this->Form->control('config.max_width', ['type' => 'number']);
echo $this->Form->control('config.max_height', ['type' => 'number']);
echo $this->Form->control('config.fixed_width', ['type' => 'number']);
echo $this->Form->control('config.fixed_height', ['type' => 'number']);

这会很好用,只是我需要用一些JS处理config.sys_file_id

我知道我必须调用$this->Form->unlockField(),但当字段是数组的一部分时,我无法找到正确的语法。到目前为止,我已经尝试过

$this->Form->unlockField('sys_file_id');
$this->Form->unlockField('config');
$this->Form->unlockField('config.sys_file_id');
$this->Form->unlockField('config[sys_file_id]');

但是该请求仍然被CCD_ 3黑洞化。

我偶然发现了这两个问题:如何;解锁';当它是hasMany关联的一部分时,CakePHP表单中的一个字段和UnlockField在CakePHP中不起作用,但它们都很旧,都引用了CakePHP 2,而我使用的是CakePHP 4。

从视图解锁输入(当是数组时(的正确语法是用点:

$this->Form->unlockField('config.sys_file_id');

https://api.cakephp.org/4.4/class-Cake.View.Helper.FormHelper.html#unlockField((

但请确保在$this->Form->create()$this->Form->end();:之间使用$this->Form->unlockField()

echo $this->Form->create();
echo $this->Form->control('config.sys_file_id', ['type' => 'number']);
echo $this->Form->control('click_enlarge', ['type' => 'checkbox']);
echo $this->Form->control('config.max_width', ['type' => 'number']);
echo $this->Form->control('config.max_height', ['type' => 'number']);
echo $this->Form->control('config.fixed_width', ['type' => 'number']);
echo $this->Form->control('config.fixed_height', ['type' => 'number']);
// before form end
$this->Form->unlockField('config.sys_file_id');

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

如果您将$this->Form->unlockField('config.sys_file_id');放置在表单end()create()之外,则会得到CakePHP异常:

FormProtector实例尚未创建。确保已加载控制器中的FormProtectionComponent,并调用在调用FormHelper::unlockField((之前,先调用FormHelper::create((。

我在CakePHP 4.4 Strawbery上测试了这段代码,它很有效。

如果JAVASCRIPT代码错误地更改了其他字段,那么问题就存在了。

我遇到了同样的问题,因为我放置了Form->unlockField('myField'(;?>在$this之前->表单->end((行,它不起作用,但我修复了在$this->表单->create((行。现在它工作了!

最新更新