我有一个带有一堆多对多复选框的表单。表单保存得很好(我检查了数据库以确保),但是复选框未与数据库一致。
我需要做一些特别的事情来让复选框保持选中状态吗?
这是我的表单定义:
<?php
namespace VNN\PressboxBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class PreferencesSportsICareAboutType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('interestingSports', 'entity', array(
'multiple' => true,
'expanded' => true,
'property' => 'name',
'class' => 'VNN\PressboxBundle\Entity\Sport',
'query_builder' => function(\VNN\PressboxBundle\Repository\SportRepository $er) {
return $er->createQueryBuilder('s')
->orderBy('s.name', 'ASC');
},
));
}
public function getName()
{
return 'vnn_pressboxbundle_preferencessportsicareabouttype';
}
}
这是我模板的相关部分:
{% block form %}
<h2>Sports I Care About</h2>
<form action="{{ path('user_update_preferences', { 'sectionName': sectionName }) }}" method="post" {{ form_enctype(form) }} novalidate class="clearfix">
{% for error in errors %}
error: {{ error.messageTemplate }}
{% endfor %}
{% for field in form.interestingSports %}
<li>
{{ form_widget(field) }}
{{ form_label(field) }}
</li>
{% endfor %}
{{ form_rest(form) }}
<p><button type="submit">Submit</button></p>
</form>
{% endblock %}