This post is just to archive a solution I used to pass an option list, to create a selectbox. The code I used to create the form type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php namespace Mb\MHoverAdBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class SettingType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('webmatchId', 'choice', array('choices' => $options['webmatchList'])) ->add('description') ->add('hash') ->add('save', 'submit'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setRequired(array('webmatchList')); } public function getName() { return 'setting'; } } |
The setDefaultOptions method makes sure you pass the array of options. If you don’t, it will throw an exception.