symfony 2 表单选择复选框列表 return 当所有复选框都未选中时无

symfony 2 form choice checkbox list return none when all checkboxes are unchecked

我在 symfony 2.3 中创建了一个带有复选框列表的表单作为选择

$fieldProp['choices'] = $values;
$fieldProp['expanded'] = true;
$fieldProp['multiple'] = true;
$fieldProp['empty_data'] = Null;
$builder->add($value2['attrId'], 'choice', $fieldProp);

如果我取消选中所有复选框并提交表单,则请求不包含该字段。 我收到的结果是

$formValues = $request->request->get($form1->getName());  

我该如何解决这个问题。

这在 W3C HTML 4 recommendation:

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful.

如果您使用的是 symfony 表单组件 - 请 use it properly

$form->handleRequest($form);
if ($form->isValid()) {
    $data = $form->getData();
}