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
.
->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
->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
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
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'
)
)
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.
Use the type entity
->add('features', 'entity', [
'class' => 'MyBundle:Feature',
'property' => 'name',
'choices' => $this->getFeatureOptionsWithGroup(),
'multiple' => true,
'required' => false,
'btn_add' => null,
])
Make sure your function getFeatureOptionsWithGroup()
returns like this
array(
'Group 1' => array(
1 => 'Feature Entity Object 1',
2 => 'Feature Entity Object 2',
),
'Group 2' => array(
3 => 'Feature Entity Object 3',
4 => 'Feature Entity Object 4',
)
)
The best way to do this is to use group_by
option.
So on your case, you need to pass name of $property
field in your FeatureType
entity then on your Property entity, you need to implement __toString()
function which should be the name of the Property as string.
Explanation :
On your FeatureType
entity table, Property association is mapped by property_id
as foreign key and Doctrine does the querying for you so you just need to specify how you want to group them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.