I'm trying to render a multiple choice selection drop-down similar to the SO question using sonata_type_model
or entity
type. I have the entity Property
with manyToMany
to Feature
that is oneToMany
of FeatureType
. The multi-choice drop-down is being implemented in the Property create form.
I have found the group_by option. But I got the warning using entity
and got the incorrect rendering using sonata_type_model
.
Attempt 1
->add('features', 'entity', array(
'class' => 'MyBundle:Feature',
'multiple' => true,
'group_by' => 'featureType'
))
Warning: Illegal offset type in isset or empty in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php at line 334
Attempt 2
->add('features', 'sonata_type_model', array(
'multiple' => true,
'btn_add' => null,
'group_by' => 'featureType'
))
It only renders the values in the drop-down such as
Attempt 3
I tried the choices
and but got error
->add('features', 'sonata_type_model', array(
'multiple' => true,
'required' => false,
'btn_add' => null,
'choices' => $this->getFeatureOptionsWithGroup()
))
Warning: get_class() expects parameter 1 to be object, array given in vendor/doctrine/common/lib/Doctrine/Common/Util/ClassUtils.php at line 58
Attempt 4
I tried the choice_list
and but got error
->add('features', 'sonata_type_model', array(
'multiple' => true,
'required' => false,
'btn_add' => null,
'choice_list' => $this->getFeatureOptionsWithGroup()
))
The option "choice_list" with value array is expected to be of type "null" or "Symfony\Component\Form\ChoiceList\ChoiceListInterface", but is of type "array" in vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php at line 888
The method getFeatureOptionsWithGroup()
returns the array like this
array(
'Group 1' => array(
1 => 'Feature 1'
2 => 'Feature 2'
),
'Group 2' => array(
3 => 'Feature 3'
4 => 'Feature 4'
)
)
Attempt 5
I updated the method using ModelChoiceList
, but got the same error of using choices
.
Warning: get_class() expects parameter 1 to be object, array given in vendor/doctrine/common/lib/Doctrine/Common/Util/ClassUtils.php at line 58
private function getFeatureOptionsWithGroup()
{
return new ModelChoiceList(
$this->modelManager,
'MyBundle:Feature',
null,
null,
$this->getRepository('MyBundle:Feature')->getFeatureOptionsWithGroup()
);
}
I hope that I would have a chance to render it without template override. Any help would be appreciated.