Symfony2渲染复选框选择字段未选中

时间:2022-10-16 18:11:17

I'm displaying a choice field in Symfony 2.0.4 with multiple checkboxes. Symfony always displays the first checkbox as checked and I would like to disable this behavior. I've tried setting the underlying entity's field to an empty array before building the form but this didn't have any effect. Is this a glitch in the Symfony version or is there a special way of achieving this?

我在Symfony 2.0.4中显示一个带有多个复选框的选项字段。 Symfony始终显示第一个复选框已选中,我想禁用此行为。在构建表单之前,我已经尝试将基础实体的字段设置为空数组,但这没有任何效果。这是Symfony版本中的一个小故障还是有一种特殊的方法来实现这一目标?

EDIT:
By choice field with multiple checkboxes I mean a choice field having the expanded and multiple options set to true.

编辑:在带有多个复选框的选项字段中,我指的是将扩展和多个选项设置为true的选择字段。

2 个解决方案

#1


1  

It seems that this was my stupid mistake. I was overriding the form theme and in the choice_widget_expanded block I was adding the checked flag for the first element of the choice array.

这似乎是我的愚蠢错误。我重写了表单主题,在choice_widget_expanded块中,我为choice数组的第一个元素添加了checked标志。

#2


0  

You need to add in your form class :

您需要在表单类中添加:

$builder->add('yourField', 'choice', array(
    'choices'     => $choices
    'required'    => false,
    ....
));

You can find more information in the ChoiceField type documentation http://symfony.com/doc/2.0/reference/forms/types/choice.html

您可以在ChoiceField类型文档中找到更多信息http://symfony.com/doc/2.0/reference/forms/types/choice.html

Also check that you don't have any JavaScript which is checking the first value.

还要检查您是否没有检查第一个值的JavaScript。

#1


1  

It seems that this was my stupid mistake. I was overriding the form theme and in the choice_widget_expanded block I was adding the checked flag for the first element of the choice array.

这似乎是我的愚蠢错误。我重写了表单主题,在choice_widget_expanded块中,我为choice数组的第一个元素添加了checked标志。

#2


0  

You need to add in your form class :

您需要在表单类中添加:

$builder->add('yourField', 'choice', array(
    'choices'     => $choices
    'required'    => false,
    ....
));

You can find more information in the ChoiceField type documentation http://symfony.com/doc/2.0/reference/forms/types/choice.html

您可以在ChoiceField类型文档中找到更多信息http://symfony.com/doc/2.0/reference/forms/types/choice.html

Also check that you don't have any JavaScript which is checking the first value.

还要检查您是否没有检查第一个值的JavaScript。