POST 数据中的意外字段具有多项选择字段名称
Unexpected field in POST data with a multiple choice fieldName
我有什么
foreach ($statuses as $key=>$value) {
echo $this->Form->control('Filter.statuses['.$key.']', array(
'type' => 'checkbox',
'value' => $key,
'label' => $value,
));
}
我得到了什么
Unexpected field 'Filter.statuses[1' in POST data
Unexpected field 'Filter.statuses[2' in POST data
Unexpected field 'Filter.statuses[3' in POST data
...
我试过的
$this->Form->unlockField('Filter.statuses');
$this->Form->unlockField('Filter.statuses[]');
如果删除 Filter.
前缀,错误就会消失,我不再需要 unlockField()
调用。
参考资料
- In cakephp 3 I got error Unexpected field in POST data
- https://book.cakephp.org/3.0/en/controllers/components/security.html
您不应该在字段名称中使用方括号,表单助手不支持。如果您需要表单助手不支持的非常规名称,请使用 name
选项指定它,同时将兼容的字段名称传递给 control()
方法的第一个参数。
全程使用点语法:
echo $this->Form->control("Filter.statuses.$key", /* ... */);
这样表单助手将能够保护字段,并创建适当的 HTML name 属性值,如 Filter[statuses][1]
.
我有什么
foreach ($statuses as $key=>$value) {
echo $this->Form->control('Filter.statuses['.$key.']', array(
'type' => 'checkbox',
'value' => $key,
'label' => $value,
));
}
我得到了什么
Unexpected field 'Filter.statuses[1' in POST data
Unexpected field 'Filter.statuses[2' in POST data
Unexpected field 'Filter.statuses[3' in POST data ...
我试过的
$this->Form->unlockField('Filter.statuses');
$this->Form->unlockField('Filter.statuses[]');
如果删除 Filter.
前缀,错误就会消失,我不再需要 unlockField()
调用。
参考资料
- In cakephp 3 I got error Unexpected field in POST data
- https://book.cakephp.org/3.0/en/controllers/components/security.html
您不应该在字段名称中使用方括号,表单助手不支持。如果您需要表单助手不支持的非常规名称,请使用 name
选项指定它,同时将兼容的字段名称传递给 control()
方法的第一个参数。
全程使用点语法:
echo $this->Form->control("Filter.statuses.$key", /* ... */);
这样表单助手将能够保护字段,并创建适当的 HTML name 属性值,如 Filter[statuses][1]
.